21

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?

Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96

3 Answers3

26

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
  • 414,610
  • 91
  • 852
  • 789
  • According to the article mentioned by David in Dakota below, the Forms.Timer sends an event when the timer goes off **and** if the UI is not already busy **and** executes on the Form thread. It does some of the synchronization work and cross-thread call for you. – Suncat2000 Mar 08 '11 at 19:09
16

MSDN had a comparison article that does the subject justice.

t3rse
  • 10,024
  • 11
  • 57
  • 84
  • 3
    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 Jan 06 '09 at 22:40
  • 2
    @t3rse: the link is dead :( – dhiraj suvarna Dec 22 '17 at 18:39
  • The article is available as a [.chm file](http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazineFebruary2004en-us.chm) of 'MSDN Magazine - February 2004'. – Sen Jacob Jul 06 '18 at 11:05
2

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 ;-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
  • That much I knew. In this particular architecture, neither a deterministic order of execution nor any particular timeliness are required. – Yes - that Jake. Jan 06 '09 at 22:38