Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Timer Constructor (TimerCallback, Object, TimeSpan, TimeSpan)

Initializes a new instance of the Timer class, using TimeSpan values to measure time intervals.

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

[SecuritySafeCriticalAttribute]
public Timer(
	TimerCallback callback,
	Object state,
	TimeSpan dueTime,
	TimeSpan period
)

Parameters

callback
Type: System.Threading.TimerCallback
A delegate that represents a method to be executed.
state
Type: System.Object
An object that contains information to be used by the callback method, or null.
dueTime
Type: System.TimeSpan
The amount of time to delay before callback is invoked. Specify -1 (negative one) milliseconds to prevent the timer from starting. Specify 0 (zero) to start the timer immediately.
period
Type: System.TimeSpan
The time interval between invocations of callback. Specify -1 (negative one) milliseconds to disable periodic signaling.

ExceptionCondition
ArgumentOutOfRangeException

The dueTime or period parameter is negative and is not equal to -1 (negative one) milliseconds.

-or-

The dueTime or period parameter is greater than 4294967294 (UInt32.MaxValue - 1) milliseconds.

ArgumentNullException

The callback parameter is null.

The delegate specified by the callback parameter is invoked once after dueTime elapses, and thereafter each time the period time interval elapses.

NoteNote:

Visual Basic users can omit the TimerCallback constructor, and simply use the AddressOf operator when specifying the callback method. Visual Basic automatically calls the correct delegate constructor.

If dueTime is 0 (zero), callback is invoked immediately. If dueTime is -1 (negative one) milliseconds, callback is not invoked; the timer is disabled but can be re-enabled by calling the Change method.

If period is 0 (zero) or -1 (negative one) milliseconds, and dueTime is positive, callback is invoked once; the periodic behavior of the timer is disabled but can be re-enabled using the Change method.

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 shows how to create a TimerCallback delegate and initialize a new instance of the Timer class. In this example, the delegate is created implicitly. Its type is inferred by the Visual Basic and C# compilers.

This example creates a timer, uses the Timer.Change method to change its interval, and then uses the Timer.Dispose method to destroy it.

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;

// The following Imports are not required for the timer. They merely simplify 
// the code.
using System.Windows.Controls;
using System.Windows.Input;

public class Example
{
   // The static Demo method sets the starting message and hooks up the handler
   // for the MouseLeftButtonUp event, which controls the demo.
   public static void Demo(TextBlock outputBlock)
   {
      outputBlock.Text += "Click to create the timer.\n";
      Example.outputBlock = outputBlock;
      outputBlock.MouseLeftButtonUp += new MouseButtonEventHandler(MouseUp);
   }

   // Data for the demo.
   private static int phase = 0;
   private static Timer t;
   private static TextBlock outputBlock;

   private static void MouseUp(object sender, MouseButtonEventArgs e)
   {
      if (phase==0)
      {
         // On the first click, create the timer.
         outputBlock.Text += "\nCreating the timer at " + 
               DateTime.Now.ToString("h:mm:ss.fff") + 
               ", to start in 1 second with a half-second interval.\n" +
               "Click to change the interval from 1/2 second to 1 second.\n\n";

         // Create a timer that invokes the callback method after one second 
         // (1000 milliseconds) and every 1/2 second thereafter. The TextBlock
         // that is used for output is passed as the state object. C# infers the 
         // delegate type, as if you had typed the following:
         //     new TimerCallback(MyTimerCallback)
         // 
         t = new Timer(MyTimerCallback, outputBlock, 
                       new TimeSpan(0, 0, 1), new TimeSpan(0, 0, 0, 0, 500));

      }
      else if (phase==1)
      {
         outputBlock.Text += "\nChanging the interval to one second.\n" +
                             "Click to destroy the timer.\n\n";
         t.Change(new TimeSpan(0), new TimeSpan(0, 0, 1));
      }
      else
      {
         // On the last click, destroy the timer and shut down the demo.
         outputBlock.Text += "\nDestroying the timer.\n" + 
                             "Refresh the page to run the demo again.";
         outputBlock.MouseLeftButtonUp -= new MouseButtonEventHandler(MouseUp);

         t.Dispose();
      }

      phase += 1;
   }

   // The static callback method is invoked on a ThreadPool thread by the Timer. In 
   // this example, the state object is not used. 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 static void MyTimerCallback(object state)
   {
      TextBlock outputBlock = (TextBlock) state;
      string msg = DateTime.Now.ToString("h:mm:ss.fff") + " MyTimerCallback was called.\n";

      outputBlock.Dispatcher.BeginInvoke(delegate () { outputBlock.Text += msg; });
   }
}

/* This example produces output similar to the following:

Click to create the timer.

Creating the timer at 4:27:38.623, to start in 1 second with a half-second interval.
Click to change the interval from 1/2 second to 1 second.

4:27:39.731 MyTimerCallback was called.
4:27:40.245 MyTimerCallback was called.
4:27:40.719 MyTimerCallback was called.

Changing the interval to one second.
Click to destroy the timer.

4:27:40.895 MyTimerCallback was called.
4:27:41.945 MyTimerCallback was called.

Destroying the timer.
Refresh the page to run the demo again.
 */


Silverlight

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

ADD
Show:
© 2015 Microsoft