Provides a mechanism for executing a method at specified intervals. This class cannot be inherited.
For a list of all members of this type, see Timer Members.
System.Object
System.MarshalByRefObject
System.Threading.Timer
[Visual Basic] NotInheritable Public Class Timer Inherits MarshalByRefObject Implements IDisposable [C#] public sealed class Timer : MarshalByRefObject, IDisposable [C++] public __gc __sealed class Timer : public MarshalByRefObject, IDisposable [JScript] public class Timer extends MarshalByRefObject implements IDisposable
This type is safe for multithreaded operations.
Use a TimerCallback delegate to specify the method you want the Timer to execute. The timer delegate is specified when the timer is constructed, and cannot be changed. The method does not execute in the thread that created the timer; it executes in a thread pool thread supplied by the system.
When you create a timer, you can specify an amount of time to wait before the first execution of the method (due time), and an amount of time to wait between subsequent executions (period). You can change these values, or disable the timer, using the Change method.
Note As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.
When a timer is no longer needed, use the Dispose method to free the resources held by the timer.
Note System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by threadpool threads. You might also consider System.Windows.Forms.Timer for use with Windows forms, and System.Timers.Timer for server-based timer functionality. These timers use events and have additional features.
[Visual Basic, C#, C++] The following example demonstrates the features of the Timer class.
[Visual Basic] Imports System Imports System.Threading Class TimerExampleState Public counter As Integer = 0 Public tmr As Timer End Class 'TimerExampleState Class App Public Shared Sub Main() Dim s As New TimerExampleState() ' Create the delegate that invokes methods for the timer. Dim timerDelegate As New TimerCallback(AddressOf CheckStatus) ' Create a timer that waits one second, then invokes every second. Dim timer As New Timer(timerDelegate, s, 1000, 1000) ' Keep a handle to the timer, so it can be disposed. s.tmr = timer ' The main thread does nothing until the timer is disposed. While Not (s.tmr Is Nothing) Thread.Sleep(0) End While Console.WriteLine("Timer example done.") End Sub 'Main ' The following method is called by the timer's delegate. Shared Sub CheckStatus(state As [Object]) Dim s As TimerExampleState = CType(state, TimerExampleState) s.counter += 1 Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, s.counter) If s.counter = 5 Then ' Shorten the period. Wait 10 seconds to restart the timer. s.tmr.Change(10000, 100) Console.WriteLine("changed...") End If If s.counter = 10 Then Console.WriteLine("disposing of timer...") s.tmr.Dispose() s.tmr = Nothing End If End Sub 'CheckStatus End Class 'App [C#] using System; using System.Threading; class TimerExampleState { public int counter = 0; public Timer tmr; } class App { public static void Main() { TimerExampleState s = new TimerExampleState(); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(CheckStatus); // Create a timer that waits one second, then invokes every second. Timer timer = new Timer(timerDelegate, s,1000, 1000); // Keep a handle to the timer, so it can be disposed. s.tmr = timer; // The main thread does nothing until the timer is disposed. while(s.tmr != null) Thread.Sleep(0); Console.WriteLine("Timer example done."); } // The following method is called by the timer's delegate. static void CheckStatus(Object state) { TimerExampleState s =(TimerExampleState)state; s.counter++; Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter); if(s.counter == 5) { // Shorten the period. Wait 10 seconds to restart the timer. (s.tmr).Change(10000,100); Console.WriteLine("changed..."); } if(s.counter == 10) { Console.WriteLine("disposing of timer..."); s.tmr.Dispose(); s.tmr = null; } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Threading; __gc class TimerExampleState { public: int counter; public: Timer* tmr; }; __gc class StatusChecker { public: // The following method is called by the timer's delegate. static void CheckStatus(Object* state) { TimerExampleState* s =dynamic_cast<TimerExampleState*>(state); s->counter++; Console::WriteLine(S" {0} Checking Status {1}.", __box(DateTime::Now.TimeOfDay), __box(s->counter)); if (s->counter == 5) { // Shorten the period. Wait 10 seconds to restart the timer. s->tmr->Change(10000, 100); Console::WriteLine(S"changed..."); } if (s->counter == 10) { Console::WriteLine(S"disposing of timer..."); s->tmr->Dispose(); s->tmr = 0; } } }; void main() { TimerExampleState* s = new TimerExampleState(); // Create the delegate that invokes methods for the timer. TimerCallback* timerDelegate = new TimerCallback(0, StatusChecker::CheckStatus); // Create a timer that waits one second, then invokes every second. Timer* timer = new Timer(timerDelegate, s, 1000, 1000); // Keep a handle to the timer, so it can be disposed. s->tmr = timer; // The main thread does nothing until the timer is disposed. while(s->tmr != 0) Thread::Sleep(0); Console::WriteLine(S"Timer example done."); }
[Visual Basic, C#, C++] This code produces the following output (the exact timings returned will vary):
08:02:09.4811456 Checking Status 1.
08:02:10.4825856 Checking Status 2.
08:02:11.4840256 Checking Status 3.
08:02:12.4854656 Checking Status 4.
08:02:13.4869056 Checking Status 5.
changed...
08:02:23.4912912 Checking Status 6.
08:02:23.5914352 Checking Status 7.
08:02:23.6915792 Checking Status 8.
08:02:23.7917232 Checking Status 9.
08:02:23.8918672 Checking Status 10.
disposing of timer...
Timer example done.
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Namespace: System.Threading
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET
Assembly: Mscorlib (in Mscorlib.dll)
Timer Members | System.Threading Namespace | TimerCallback |