views:

220

answers:

2

There are three Timer classes in .net (Timers.Timer, Threading.Timer and Windows.Forms.Timer). Which should be used when? and What are performance implication of using them?

+1  A: 

See if this helps.

EDIT: Other than above, I think the timers differ in their interval capacity.
I remember having heard of it on one of the dotnetrocks show. I will try to find that out.

EDIT2: Also look at caveats added by people on MSDN page for each of the classes.
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx (this says, it is optimized for windows application. Hence, should be used in windows app).

IMO, the timers differ in their functionality. So, you will have to decide which one suits your need, keeping above in mind

shahkalpesh
A: 

As far as I know, there are no performance differences between them -- not when compared to the time required to execute the logic you'll perform inside the timer elapsed event handler. The important differences are:

  • Timers.Timer and Threading.Timer raise events from a separate Threading.ThreadPool thread.
  • Forms.Timer raises events from within the UI thread.
  • Timers.Timer can be placed as a component on a Form.
  • Forms.Timer has a more user friendly interface.

Picking one over the other depends on your needs. If your response to the timer elapsed event is a kind of background job must occur independent of the UI, then you'll want either Timers.Timer or Threading.Timer. If your response to a timer elapsed event is some sort of visual feedback, then you'll want Forms.Timer.

Personally, I would create a custom timer abstraction/interface and create three implementations based on the .NET timers. This will allow you to, in theory, switch between the different types of timers without any change to your code.