System.Threading.Timer Class

Provides a mechanism for executing a method at specified intervals. This class cannot be inherited.

See Also: Timer Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Timer : MarshalByRefObject, IDisposable

Remarks

Use a System.Threading.TimerCallback delegate to specify the method you want the System.Threading.Timer to execute. The timer delegate is specified when the timer is constructed, and cannot be changed. The method does not execute on the thread that created the timer; it executes on a System.Threading.ThreadPool 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 Timer.Change(int, int) method.

Note:

As long as you are using a System.Threading.Timer, you must keep a reference to it. As with any managed object, a System.Threading.Timer is subject to garbage collection when there are no references to it. The fact that a System.Threading.Timer is still active does not prevent it from being collected.

When a timer is no longer needed, use the erload:System.Threading.Timer.Dispose method to free the resources held by the timer. Note that callbacks can occur after the Timer.Dispose method overload has been called, because the timer queues callbacks for execution by thread pool threads. You can use the Timer.Dispose(WaitHandle) method overload to wait until all callbacks have completed.

The callback method executed by the timer should be reentrant, because it is called on System.Threading.ThreadPool threads. The callback can be executed simultaneously on two thread pool threads if the timer interval is less than the time required to execute the callback, or if all thread pool threads are in use and the callback is queued multiple times.

Note:

System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates the features of the System.Threading.Timer class.

C# Example

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;
    }
   }
}

An example of some output is

10:51:40.5809015 Checking Status 1.
10:51:41.5823515 Checking Status 2.
10:51:42.5838015 Checking Status 3.
10:51:43.5852515 Checking Status 4.
10:51:44.5867015 Checking Status 5.
changed...
10:51:54.5911870 Checking Status 6.
10:51:54.6913320 Checking Status 7.
10:51:54.7914770 Checking Status 8.
10:51:54.8916220 Checking Status 9.
10:51:54.9917670 Checking Status 10.
disposing of timer...
Timer example done.

The exact timings returned by this example will vary.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0