112

Why are there five timer classes in the .Net framework, namely the following:

  1. System.Timers.Timer
  2. System.Threading.Timer
  3. System.Windows.Forms.Timer
  4. System.Web.UI.Timer
  5. System.Windows.Threading.DispatcherTimer

Why are there several versions of the Timer class? And what are the differences between them?

6
  • 4
    There are actually 4 Timer classes, you're forgetting System.Windows.Forms.Timer and System.Web.UI.Timer (and also System.Windows.Threading.DispatcherTimer, but it has a different name...) Commented Apr 25, 2012 at 13:51
  • 1
    @ThomasLevesque, thank you pointing that out, I updated the question. Commented Apr 25, 2012 at 13:57
  • 2
    They all behave differently, example System.Threading.Timer spawns a new thread for each time it tick.
    – Tenerezza
    Commented Apr 25, 2012 at 13:57
  • 2
    Here you go: Comparing the Timer Classes in the .NET Framework Class Library web.archive.org/web/20150329101415/https://msdn.microsoft.com/… Contents - System.Windows.Forms.Timer - System.Timers.Timer - System.Threading.Timer - Thread-safe Programming with Timers - Dealing with Timer Event Reentrance - Conclusion Commented Apr 25, 2012 at 14:03
  • 6
    @MitchWheat, I took a look on the question you provided, it discusses only 2 out 5, hence its not a duplicate. Commented Apr 25, 2012 at 14:04

3 Answers 3

51

Here's a description of the primary timers and the points that i find to be the most noteworthy.

Winforms.Timer

  • ticks on UI thread
  • ticks delayed until UI thread is idle
  • will skip ticks if the UI thread is busy long enough for multiple ticks to pile up

DispatcherTimer

  • invoked on UI thread
  • can set priority for what level of 'idle' is required to generate a tick
  • will skip ticks if they pile up

Threading.Timer

  • ticks on a worker thread from threadpool - no option for specifying thread
  • ticks are always fired on time
  • none are skipped - you must guard against new ticks while you're still processing a former tick
  • unhandled exceptions will crash the application

Timers.Timer

  • wrapper around threading timer
  • ticks on a worker thread taken from the CLR threadpool
  • can force to tick on a specific thread by supplying a SynchronizationObject
  • ticks are always fired on time
  • none are skipped
  • silently eats exceptions
2
12

Timers.Timer generates an event after a set interval, with an option to generate recurring events. MSDN

Windows.Forms.Timer is a Control for winforms.

Web.UI.Timer performs asynchronous or synchronous Web page postbacks at a defined interval. MSDN

Threading.Timer is the timer for Callbacks. Creates a new Thread for working. Served by thread pool threads. MSDN

So, these timers have different purposes, also they are served by different tools.

4

This question is now answered by Microsoft in the MSDN article for system.timers.timer

https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=net-5.0

Tip

Be aware that .NET includes four classes named Timer, each of which offers different functionality:

System.Timers.Timer (this topic): fires an event at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.

System.Threading.Timer: executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.

System.Windows.Forms.Timer (.NET Framework only): a Windows Forms component that fires an event at regular intervals. The component has no user interface and is designed for use in a single-threaded environment.

System.Web.UI.Timer (.NET Framework only): an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.