Skip to content

Comparing System Timers

Outlining Three System Timers

I have seen a few samples of code involving the three timer classes provided with the .Net framework over the years.  Juval has such a sample, which he is updating to use ISynchronizeInvoke.InvokeRequired <code>.  Even within the MSDN documentation, however, I have never seen much guidance between when to use </code> System.Threading.Timer <code> and </code> System.Timers.Timer .  I decided to put together a table that outlines the characteristics of all three.

Feature descriptionSystem.Timers.TimerSystem.Threading.TimerSystem.Windows.Forms.Timer
Support for adding and removing listeners after the timer is instantiated.YesNoYes
Supports call backs on the user-interface threadYesNoYes
Calls back from threads obtained from the thread poolYesYesNo
Supports drag-and-drop in the Windows Forms DesignerYesNoYes
Suitable for running in a server multi-threaded environmentYesYesNo
Includes support for passing arbitrary state from the timer initialization to the callback.NoYesNo
Implements IDisposableYesYesYes
Supports one-off callbacks as well as periodic repeating callbacksYesYesYes
Accessible across application domain boundariesYesYesYes
Supports IComponent – hostable in an IContainerYesNoYes

Using the System.Windows.Forms.Timer <code> is a relatively obvious choice for user interface programming.  Choosing between the other two options is less obvious and generally the choice between the two is insignificant.  If hosting within an </code> IContainer <code> is necessary then obviously </code> System.Timers.Timer <code> is the right choice.  However, if no specific </code> System.Timers.Timer <code> feature is required, then I suggest choosing </code> System.Threading.Timer by default, simply because it is a slightly lighter weight implementation.

UPDATE – October 5, 2005

Thanks to Stephen Toub for pointing out that System.Timers.Timer <code> can be used in the Windows Forms designer (although it does not appear in the toolbox by default) and that, using </code> SynchronizationObject <code>, on </code> System.Timers.Timer , there is support for calling back on the UI thread.

1 thought on “Comparing System Timers”

Leave a Reply

Your email address will not be published. Required fields are marked *