Timer Constructor (TimerCallback)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the Timer class with an infinite period and an infinite due time, using the newly created Timer object as the state object.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public Timer(
	TimerCallback callback


Type: System.Threading.TimerCallback

A TimerCallback delegate representing a method to be executed.

Call this constructor when you want to use the Timer object itself as the state object. After creating the timer, use the Change method to set the interval and due time.

This constructor specifies an infinite due time before the first callback and an infinite interval between callbacks, in order to prevent the first callback from occurring before the Timer object is assigned to the state object.

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

The following code example creates a new timer, using the timer itself as the state object. The Change method is used to start the timer. When the timer callback occurs, the state object is used to turn the timer off.

using System;
using System.Threading;

public class Example
    public static void Main()
        // Create an instance of the Example class, and start two
        // timers.
        Example ex = new Example();

        Console.WriteLine("Press Enter to end the program.");

    public void StartTimer(int dueTime)
        Timer t = new Timer(new TimerCallback(TimerProc));
        t.Change(dueTime, 0);

    private void TimerProc(object state)
        // The state object is the Timer object.
        Timer t = (Timer) state;
        Console.WriteLine("The timer callback executes.");

.NET Framework
Available since 2.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top