using System;
using System.Threading;
class Program
{
static void Main()
{
// Timer timer = new Timer(TimerCallback, null, delayMilliseconds, intervalMilliseconds);
// You can pass an optional state object (of type object) that will be passed to the callback method. If you don't need it, you can pass null.
Timer timer = new Timer(TimerCallback, null, 0, 1000); // Execute every 1 second
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
timer.Change(delayMilliseconds, intervalMilliseconds);
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
private static void TimerCallback(object state)
{
Console.WriteLine("Timer elapsed at: " + DateTime.Now);
}
}
using System;
using System.Timers;
class Program
{
static void Main()
{
Timer timer = new Timer();
timer.Interval = 1000; // 1 second
timer.Elapsed += TimerElapsedEventHandler;
timer.Start();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
timer.Stop();
}
private static void TimerElapsedEventHandler(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Timer elapsed at: " + DateTime.Now.ToString("HH:mm:ss"));
}
}
System.Threading.Timer
and System.Timers.Timer
are two timer classes in .NET that allow you to execute code at specified intervals. They have some differences in terms of usage and behavior:
-
Namespace:
System.Threading.Timer
is part of theSystem.Threading
namespace.System.Timers.Timer
is part of theSystem.Timers
namespace.
-
Threading Model:
System.Threading.Timer
is a lightweight timer that uses the thread pool for executing its timer callbacks. This means that the callback method you provide to this timer runs on a thread from the thread pool.System.Timers.Timer
, on the other hand, is a more feature-rich timer and is designed to work with multi-threaded applications. It uses its own thread for executing timer callbacks.
-
Callback Delegate:
System.Threading.Timer
uses aTimerCallback
delegate for its callback method, which takes anObject
parameter.System.Timers.Timer
uses anElapsedEventHandler
delegate for its callback method, which takes two parameters: the sender (usually the timer itself) and anElapsedEventArgs
object.
-
Synchronization Context:
System.Timers.Timer
captures and propagates the synchronization context of the thread that created it. This means that if you create aSystem.Timers.Timer
on an UI thread, the timer’s callback will run on the UI thread by default.System.Threading.Timer
does not capture or propagate the synchronization context, so its callback will run on a thread pool thread without any synchronization context.
-
Accuracy:
System.Timers.Timer
is generally more accurate thanSystem.Threading.Timer
because it uses a separate thread for timer operations. However, its accuracy can still be affected by system load and other factors.
-
Start and Stop:
- Both timers provide methods to start and stop the timer. In
System.Threading.Timer
, you can use theChange
method to modify the due time and interval after the timer is created. System.Timers.Timer
providesStart
andStop
methods for starting and stopping the timer, and you can change the interval and enable or disable it without recreating the timer.
- Both timers provide methods to start and stop the timer. In
-
Disposed Event:
System.Timers.Timer
includes aDisposed
event that can be useful for cleanup operations when the timer is disposed of.
In summary, the choice between System.Threading.Timer
and System.Timers.Timer
depends on your specific requirements and the threading model of your application. If you need a simple timer for background tasks and are comfortable with thread pool behavior, System.Threading.Timer
may be sufficient. If you need more control, accuracy, or want to run the timer on a specific synchronization context (such as a UI thread), System.Timers.Timer
might be a better choice.