.NET Framework 2.2 has 3 types of “Timer” control, they are
a) System.Windows.Form.Timer: This is probably the mostly used “Timer” control, we can add it by drag and drop form the toolbox.. This type of timer requires that a user interface message pump is operating[this type of timer corresponds to the WM_TIMER message used in Windows SDK programming, and is implemented by this Timer class] , and all timer events are handled on the UI thread, so upon “Timer_Tick” event, if we need to do modify the UI, then this type of Timer is appropriate[ if accuracy and UI responsiveness is not a concern ].
b) System.Timers.Timer: This type of timer can be used in any type of .NET program, including Windows Forms programs, Windows Services, ASP.NET programs, console applications, Web services, and others. The timer works in much the same way as does the Windows Forms timer, but does not require the Windows message pump. Other than that, the primary difference between server timers and Windows Forms timers is that the event handlers for server timers execute on thread pool threads. This makes it possible to maintain a responsive user interface even if the event handler takes a long time to execute.
c) System.Threading.Timer: This timer class implements a simple lightweight timer that uses callback functions and is served by thread-pool threads. This type of timer is not a component like the other two. It also has fewer features. It has the benefit, though, of not going through the event system, but rather making direct callbacks. When we create the timer, we specify the callback method, the time to wait before the first time the callback is executed, and the amount of time (period) between calls.
As per the MSDN documentation type b i.e. Timers,Timer is the most accurate timer, I have written a “Test application” to do various test, and I have found the timers [I have tested with all 3 type of timers ] have an accuracy up-to 10 milliseconds, although it has been mentioned in the MSDN documentation that System.Timers has accuracy of 55 millisecond.
If anybody want to play with the TestApplication please download it from "MS Developer's group, kolkata" site [ http://groups.msn.com/Kolkatanet/general.msnw?action=get_message&mview=0&ID_Message=2381 ]
To run the Test application, select a Timer type, specify the interval value( in millisecond ), and hit on the “Start” button, on each timer_tick, the handler/callback routine will print the current time, stop the timer using the “Stop” button.
Important note: Since “tick” event of Timer.Timer and Threading.Timer get handled by the thread-pool thread, so accessing the UI thread form the event-handlers or the callback method will raise exception , which is why I have set the “CheckForIllegalCrossThreadCalls” as false.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment