System.Threading.Timer - Simplest multithreaded timer. Contains two methods and one constructor.
Example: A timer calls the DataWrite method, which writes “multithread executed…” after five seconds have elapsed, and then every second after that until the user presses Enter:
using System;
using System.Threading;
class Program
{
static void Main()
{
// First interval = 5000ms; subsequent intervals = 1000ms
Timer timer = new Timer (DataWrite, "multithread executed...", 5000, 1000);
Console.ReadLine();
timer.Dispose(); // This both stops the timer and cleans up.
}
static void DataWrite (object data)
{
// This runs on a pooled thread
Console.WriteLine (data); // Writes "multithread executed..."
}
}
Note : Will post a separate section for disposing multithreaded timers.
Change - This method can be called when you would like change the timer interval.
Timeout.Infinite - If you want to fire just once. Specify this in the last argument of the constructor.
System.Timers - Another timer class provided by .NET Framework. It wraps the System.Threading.Timer.
IComponent - Allowing it to be sited in the Visual Studio’s Designer’s component trayInterval property instead of a Change methodElapsed event instead of a callback delegateEnabled property to start and stop the timer (default value = false)Start & Stop methods in case if you get confused by Enabled property (above point)AutoReset - for indicating a recurring event (default value = true)SynchronizingObject property with Invoke and BeginInvoke methods for safely calling methods on WPF elements and Windows Forms controlsExample representing all the above features:
using System;
using System.Timers; // Timers namespace rather than Threading
class SystemTimer
{
static void Main()
{
Timer timer = new Timer(); // Doesn't require any args
timer.Interval = 500;
timer.Elapsed += timer_Elapsed; // Uses an event instead of a delegate
timer.Start(); // Start the timer
Console.ReadLine();
timer.Stop(); // Stop the timer
Console.ReadLine();
timer.Start(); // Restart the timer
Console.ReadLine();
timer.Dispose(); // Permanently stop the timer
}
static void timer_Elapsed(object sender, EventArgs e)
{
Console.WriteLine ("Tick");
}
}
Multithreaded timers - use the thread pool to allow a few threads to serve many timers. It means that callback method or Elapsed event may trigger on a different thread each time it is called.
Elapsed - this event always fires on time—regardless of whether the previous Elapsed event finished executing. Because of this, callbacks or event handlers must be thread-safe. The accuracy of multithreaded timers depends on the OS, and is typically in the 10–20 ms.
interop - when ever you need greater accuracy use this and call the Windows multimedia timer. This has accuracy down to 1 ms and it is defined in winmm.dll.