Timer Constructor (TimerCallback)

Initializes a new instance of the Timer class with an infinite interval 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 delegate that represents 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 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.

The example displays its output in a TextBlock on the UI thread. To access the TextBlock from the callback thread, the example uses the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then uses the Dispatcher.BeginInvoke method to make the cross-thread call.

using System;
using System.Threading;

public class Example
    private static System.Windows.Controls.TextBlock outputBlock;

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
        Example.outputBlock = outputBlock;
        outputBlock.Text += "Refresh the page to run the example again.\n";

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

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

    // The callback method is invoked on a ThreadPool thread by the Timer. In 
    // order to update the TextBlock object, which is on the UI thread, you must 
    // make the cross-thread call by using the Dispatcher object that is 
    // associated with the TextBlock.
    private void TimerProc(object state)
        // The state object is the Timer object.
        Timer t = (Timer) state;

        Example.outputBlock.Dispatcher.BeginInvoke(delegate () { 
               Example.outputBlock.Text += "Timer callback executes.\n"; });

/* This example produces the following output:

Refresh the page to run the example again.
Timer callback executes.
Timer callback executes.


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions