A cacophony of ramblings from my potpourri of notes
|
Subscribe
| |  |
Search
Categories
On this page...
| January, 2009 (4) |
| December, 2008 (4) |
| November, 2008 (2) |
| October, 2008 (2) |
| September, 2008 (4) |
| August, 2008 (2) |
| July, 2008 (3) |
| June, 2008 (9) |
| May, 2008 (3) |
| December, 2007 (1) |
| October, 2007 (1) |
| August, 2007 (1) |
| July, 2007 (1) |
| June, 2007 (3) |
| April, 2007 (3) |
| February, 2007 (4) |
| January, 2007 (1) |
| December, 2006 (1) |
| November, 2006 (3) |
| October, 2006 (5) |
| September, 2006 (4) |
| August, 2006 (4) |
| July, 2006 (6) |
| May, 2006 (1) |
| March, 2006 (1) |
| February, 2006 (7) |
| January, 2006 (1) |
| November, 2005 (4) |
| October, 2005 (7) |
| September, 2005 (7) |
| August, 2005 (7) |
| July, 2005 (9) |
| June, 2005 (12) |
| May, 2005 (3) |
| April, 2005 (8) |
| March, 2005 (8) |
| February, 2005 (10) |
| January, 2005 (3) |
| December, 2004 (4) |
| November, 2004 (1) |
| September, 2004 (4) |
| August, 2004 (1) |
| July, 2004 (1) |
| June, 2004 (8) |
| May, 2004 (5) |
| April, 2004 (16) |
| March, 2004 (7) |
| February, 2004 (13) |
| January, 2004 (16) |
| December, 2003 (17) |
| November, 2003 (13) |
| October, 2003 (13) |
| September, 2003 (30) |
| August, 2003 (33) |
| July, 2003 (66) |
| June, 2003 (29) |
| May, 2003 (48) |
| April, 2003 (83) |
| March, 2003 (26) |
| February, 2003 (23) |
| January, 2003 (31) |
| December, 2002 (14) |
| November, 2002 (19) |
| October, 2002 (13) |
|

Saturday, February 19, 2005
System.Windows.Forms.Timer vs. System.Threading.Timer vs. System.Timers.Timer
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. Even within the MSDN documentation, however, I have never seen much guidance between when to use System.Threading.Timer and System.Timers.Timer. I decided to put together a table that outlines the characteristics of all three.
| Feature description |
System.Timers.Timer |
System.Threading.Timer |
System.Windows.Forms.Timer |
| Support for adding and removing listeners after the timer is instantiated. |
Yes |
No |
Yes |
| Supports call backs on the user-interface thread |
Yes |
No |
Yes |
| Calls back from threads obtained from the thread pool |
Yes |
Yes |
No |
| Supports drag-and-drop in the Windows Forms Designer |
Yes |
No |
Yes |
| Suitable for running in a server multi-threaded environment |
Yes |
Yes |
No |
| Includes support for passing arbitrary state from the timer initialization to the callback. |
No |
Yes |
No |
| Implements IDisposable |
Yes |
Yes |
Yes |
| Supports one-off callbacks as well as periodic repeating callbacks |
Yes |
Yes |
Yes |
| Accessible across application domain boundaries |
Yes |
Yes |
Yes |
| Supports IComponent – hostable in an IContainer |
Yes |
No |
Yes |
Using the System.Windows.Forms.Timer 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 IContainer is necessary then obviously System.Timers.Timer is the right choice. However, if no specific System.Timers.Timer feature is required, then I suggest choosing 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 can be used in the Windows Forms designer (although it does not appear in the toolbox by default) and that, using SynchronizationObject, on System.Timers.Timer, there is support for calling back on the UI thread.
Saturday, February 19, 2005 9:20:02 PM (Pacific Standard Time, UTC-08:00)
Computer Related | .Net