SoFunction
Updated on 2025-03-06

Explanation on single-threading and multi-threading usage of timer timer in .NET Framework

If you need to repeat some methods with regular time intervals, the easiest way is to use a timer. Compared with the example below, timers can use memory and resources easily and efficiently:

new Thread (delegate() {
             while (enabled)
             {
              DoSomeAction();
               ( (24));
             }
            }).Start();

Not only does this permanently take up a thread, but DoSomeAction happens later every day without additional code. Timers resolve these issues.

.NET Framework provides 4 timers. The following two classes are general multithreaded timers:

(1)
(2)
The other two are dedicated single-threaded timers:

(3) (Timer for Windows Forms)
(4) (WPF timer)
Multithreaded timers are more powerful, precise and flexible, while single-threaded timers are safe and more convenient for some simple tasks to update Windows Forms and WPF controls.

1. Multithreaded timer Permalink

It is the simplest multithreaded timer: it has only one constructor and two common methods (pleasing to the minimalist, and the author of this book!). In the next example, a timer calls the Tick method after 5 seconds to print "tick..." and then prints once every second until the user presses Enter:

using System;
using ;

class Program
{
 static void Main()
 {
  // The first interval is 5000ms, and the next interval is 1000ms  Timer tmr = new Timer (Tick, "tick...", 5000, 1000);
  ();
  ();     // Stop the timer and perform cleanup work }

 static void Tick (object data)
 {
  // This runs on a thread pool thread   (data);     // Print "tick..." }
}

The time interval of the timer can then be changed by calling the Change method. If you want the timer to fire only once, you can specify the last parameter to the constructor.

.NET Framework provides another timer class with the same name under the namespace. It just encapsulated and provides additional convenience with the exact same underlying engine. Here is an introduction to the added functions:

(1) Implemented Component, which is allowed to be used in Visual Studio's designer.
(2) The Interval property replaces the Change method.
(3) The Elapsed event replaces the callback delegation.
(4) The Enabled property is used to start or stop the timer (the default value is false).
(5) Start and Stop methods to avoid confusion about the Enabled attribute.
(6) AutoReset ID to specify whether it is a repeatable event (default is true).
The SynchronizingObject property provides Invoke and BeginInvoke methods for safely calling methods on WPF and Windows Forms controls.
Here is an example:

using System;
using ;  // Namespace is Timers instead of Threading
class SystemTimer
{
 static void Main()
 {
  Timer tmr = new Timer();    // No parameters are required   = 500;
   += tmr_Elapsed;  // Use events instead of delegate  ();          // Turn on the timer  ();
  ();          // Stop the timer  ();
  ();          // Restart the timer  ();
  ();         // Permanently stop the timer }

 static void tmr_Elapsed (object sender, EventArgs e)
 {
   ("Tick");
 }
}

Multithreaded timers use thread pools to allow a small number of threads to serve multiple timers. This means that the callback method or Elapsed event may be fired on a different thread each time. In addition, Elapsed is always triggered almost on time regardless of whether the previous Elapsed is completed or not. Therefore, the callback method or event handler must be thread-safe.

The accuracy of multithreaded timers depends on the operating system, usually in the range of 10-20 ms. If higher precision is required, you can use native interop to call the Windows multimedia timer, which can increase the accuracy to 1 ms. It is defined in , first call timeBeginPeriod to notify the operating system that you need higher timer accuracy, and then call timeSetEvent to start the multimedia timer. When the use is completed, call timeKillEvent to stop the timer, and finally call timeEndPeriod to notify the operating system that you no longer need higher timer accuracy. The complete example can be found online by searching for the keyword dllimport timesetevent.

2. Single-threaded timer Permalink

The .NET Framework provides two timers designed to eliminate thread safety issues in WPF and Windows Forms applications:

(WPF)
(Windows Forms)
Single-threaded timers are not designed to work outside their specific environment. For example, if you use the Windows Forms timer in a Windows System Services application, the Timer event will not fire!

The members they exposed are all the same (Interval, Tick, Start, and Stop), and are used similarly. But the difference is how it works internally. They do not use thread pools to generate timer events, and WPF and Windows Forms timers rely on the underlying message pumping mechanism of the UI model. It means that the Tick event is always triggered by the thread that creates the timer, and in a normal program, it is the thread that manages all UI elements and controls. There are many benefits to this:

Single-threaded timers are safer and more convenient for simple tasks such as updating Windows Forms controls or WPF. SynchronizingObject object that safely calls methods in WPF or Windows Forms.
Single-threaded timers are timers designed to belong to their execution environment. If you use Windows Forms Timer in a Windows service application, the timer event will not be triggered, and will only be triggered in the corresponding environment.
Like, they also provide the same members (Interval, Tick, Start, Stop), but their internal workings are different. WPF and Windows Forms timers use message loop mechanisms to replace the thread pool's mechanism for generating messages.

You may not have to consider thread safety.
A new Tick will not be triggered until the previous Tick has completed execution.
You can update the UI control directly in the processing code of the Tick time event without calling or.
This sounds unbelievable until you realize that the program using these timers is not really multithreaded and will not be executed in parallel. A thread serves all timers and also processes UI events. This brings the disadvantages of single-threaded timers:

Unless the Tick event processor executes very quickly, the UI will lose its response.
This makes WPF and Windows Forms timers only work for small tasks, usually those that update the appearance of the UI (for example, displaying a clock or countdown). Otherwise, you need a multi-threaded timer.

In terms of accuracy, single-threaded timers are similar to multi-threaded timers (ten milliseconds), but are usually less accurate because they are delayed by other UI requests (or other timer events).

A single-threaded timer is based on a Windows message loop, and the application synchronizes the timer's messages. You will find that the UI interface is slower. The solution to this problem is to use a multithreaded timer.
Disadvantages of single-threaded timers: Unless the processing code of the Tick event is executed very quickly, the UI interface will become very slow to respond. So both WPF and Windows Forms timers are very suitable for small tasks, especially for tasks that update interfaces. For example, clock and count display. Otherwise, you need a multi-threaded timer