views:

1218

answers:

4

The short form of this question: When, if ever, is it appropriate to use the Forms.Timer in a multithreaded WinForms application?

More specifically, I am architecting an application that uses multiple System.Threading.Timers to launch processes asynchronously, check queues containing the results of those asynchronous processes, and update the statistics to be shown by the application's main form.

In an application like that, is it appropriate to use a Forms.Timer to actually check the application statistics and draw them to the main form or would that just throw a wrench into the application's smooth running?

+8  A: 

MSDN had a comparison article that does the subject justice.

David in Dakota
Decent article. For the purposes of this discussion, it'd be nice if you'd summarize the differences given between the two (Threading.Timer requires synchronization but provides more control, Forms.Timer won't necessarily generate an event for every tick, etc.)
Shog9
Excellent. Onto my "to read" pile that goes.
Jekke
+9  A: 

Forms.Timer registers the timer to send a message when the timer goes off. The event raised is treated like a GUI event. System.Threading.Timer uses a thread blocking approach. Basically, you should use Forms.Timer for your GUI oriented timers (to add some effects to the user interface) and Threading.Timer for scheduling tasks and other things.

Mehrdad Afshari
+2  A: 

I agree with what Mehrdad and David said, but you should be aware that timers offer no guarantee of timeliness or order or execution. Too many timers and the application will just hang ;-)

Steven A. Lowe
That much I knew. In this particular architecture, neither a deterministic order of execution nor any particular timeliness are required.
Jekke
+2  A: 

Since .NET Framework 3.0 we also have System.Windows.Threading.DispatcherTimer at our disposal. Here's how I used it:

timer.Interval = New TimeSpan(0, 0, 1)
timer.IsEnabled = True
AddHandler timer.Tick, AddressOf UpdateUI
timer.Start()

I know the original question didn't include this class, and was oriented on win forms. However, for a WPF app I'm working on I had the same dillema, and I found this DispatcherTimer works well.

Dragoljub