BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.MarshalByRefObject System.IDisposable System.Runtime.InteropServices.ComVisible(true)

The following example demonstrates the features of the class.

`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.

Use a delegate to specify the method you want the 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 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 method.

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

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

The callback method executed by the timer should be reentrant, because it is called on 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.

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. is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using , which raises events and has additional features.

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

- Constructor
  2.0.0.0
  4.0.0.0

  Call this constructor when you want to use the object itself as the state object. After creating the timer, use the 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 object is assigned to the state object.

  The method specified for should be reentrant, because it is called on 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.

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

  A delegate representing a method to be executed.
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  or is negative and is not equal to .
  is a reference.

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

  [Visual Basic]

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

  If is zero (0), is invoked immediately. If is , is not invoked; the timer is disabled, but can be re-enabled by calling the method.

  If is zero (0) or and is not , is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled using the method.

  The method specified for should be reentrant, because it is called on 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.

  Initializes a new instance of the Timer class, using a 32-bit signed integer to specify the time interval.

  A delegate representing a method to be executed.
  An object containing information to be used by the callback method, or null.
  The amount of time to delay before is invoked, in milliseconds. Specify to prevent the timer from starting. Specify zero (0) to start the timer immediately.
  The time interval between invocations of , in milliseconds. Specify to disable periodic signaling.
  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

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

  [Visual Basic]

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

  If is zero (0), is invoked immediately. If is , is not invoked; the timer is disabled, but can be re-enabled by calling the method.

  If is zero (0) or and is not , is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled using the method.

  The method specified for should be reentrant, because it is called on 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.

  Initializes a new instance of the Timer class, using 64-bit signed integers to measure time intervals.

  A delegate representing a method to be executed.
  An object containing information to be used by the callback method, or null.
  The amount of time to delay before is invoked, in milliseconds. Specify to prevent the timer from starting. Specify zero (0) to start the timer immediately.
  The time interval between invocations of , in milliseconds. Specify to disable periodic signaling.
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  The number of milliseconds in the value of or is negative and not equal to , or is greater than .
  is a reference.

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

  [Visual Basic]

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

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

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

  The method specified for should be reentrant, because it is called on 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.

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

  A delegate representing a method to be executed.
  An object containing information to be used by the callback method, or null.
  The representing the amount of time to delay before the parameter invokes its methods. Specify negative one (-1) milliseconds to prevent the timer from starting. Specify zero (0) to start the timer immediately.
  The time interval between invocations of the methods referenced by . Specify negative one (-1) milliseconds to disable periodic signaling.
  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.CLSCompliant(false)

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

  [Visual Basic]

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

  If is zero (0), is invoked immediately. If is , is not invoked; the timer is disabled, but can be re-enabled by calling the method.

  If is zero (0) or and is not , is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled using the method.

  The method specified for should be reentrant, because it is called on 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.

  Initializes a new instance of the Timer class, using 32-bit unsigned integers to measure time intervals.

  A delegate representing a method to be executed.
  An object containing information to be used by the callback method, or null.
  The amount of time to delay before is invoked, in milliseconds. Specify to prevent the timer from starting. Specify zero (0) to start the timer immediately.
  The time interval between invocations of , in milliseconds. Specify to disable periodic signaling.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` or is negative and is not equal to .

  The callback method is invoked once after elapses, and thereafter each time the time interval specified by elapses.

  If is zero (0), the callback method is invoked immediately. If is , the callback method is never invoked; the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  If is zero (0) or , and is not Infinite, the callback method is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals.

  true if the timer was successfully updated; otherwise, false.

  The amount of time to delay before the invoking the callback method specified when the was constructed, in milliseconds. Specify to prevent the timer from restarting. Specify zero (0) to restart the timer immediately. The time interval between invocations of the callback method specified when the was constructed, in milliseconds. Specify to disable periodic signaling. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` ``

  The callback method is invoked once after elapses, and thereafter each time the time interval specified by elapses.

  If is zero (0), the callback method is invoked immediately. If is , the callback method is never invoked; the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  If is zero (0) or , and is not Infinite, the callback method is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  Changes the start time and the interval between method invocations for a timer, using 64-bit signed integers to measure time intervals.

  true if the timer was successfully updated; otherwise, false.

  The amount of time to delay before the invoking the callback method specified when the was constructed, in milliseconds. Specify to prevent the timer from restarting. Specify zero (0) to restart the timer immediately. The time interval between invocations of the callback method specified when the was constructed, in milliseconds. Specify to disable periodic signaling.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` or is negative and is not equal to .

  The callback method is invoked once after elapses, and thereafter each time the time interval specified by elapses.

  If is zero (0), the callback method is invoked immediately. If is negative one (-1) milliseconds, the callback method is never invoked; the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  If is zero (0) or negative one (-1) milliseconds, and is positive, the callback method is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled by calling and specifying a value greater than zero for .

  Changes the start time and the interval between method invocations for a timer, using values to measure time intervals.

  true if the timer was successfully updated; otherwise, false.

  A representing the amount of time to delay before invoking the callback method specified when the was constructed. Specify negative one (-1) milliseconds to prevent the timer from restarting. Specify zero (0) to restart the timer immediately. The time interval between invocations of the callback method specified when the was constructed. Specify negative one (-1) milliseconds to disable periodic signaling. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.CLSCompliant(false)
  System.Boolean `` ``

  The callback method is invoked once after elapses, and thereafter each time the time interval specified by elapses.

  If is zero (0), the callback method is invoked immediately. If is , the callback method is never invoked; the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  If is zero (0) or , and is not Infinite, the callback method is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled by calling and specifying a positive value for .

  Changes the start time and the interval between method invocations for a timer, using 32-bit unsigned integers to measure time intervals.

  true if the timer was successfully updated; otherwise, false.

  The amount of time to delay before the invoking the callback method specified when the was constructed, in milliseconds. Specify to prevent the timer from restarting. Specify zero (0) to restart the timer immediately. The time interval between invocations of the callback method specified when the was constructed, in milliseconds. Specify to disable periodic signaling.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Void

  Calling Dispose allows the resources used by the to be reallocated for other purposes. For more information about Dispose, see [<topic://cpconCleaningUpUnmanagedResources>].

  Callbacks can occur after the method overload has been called, because the timer queues callbacks for execution by thread pool threads. You can use the method overload to wait until all callbacks have completed.

  Releases all resources used by the current instance of .

  0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` is .

  Calling Dispose allows the resources used by the to be reallocated for other purposes. For more information about Dispose, see [<topic://cpconCleaningUpUnmanagedResources>].

  When this method completes, it signals the specified by the parameter. Use this overload of the method if you want to be able to block until you are certain that the timer has been disposed. The timer is not disposed until all currently queued callbacks have completed.

  If the callback uses the method to set the parameter to zero, a race condition can occur when the method overload is called: If the timer queues a new callback before the method overload detects that there are no callbacks queued, continues to block; otherwise, the timer is disposed while the new callback is being queued, and an is thrown when the new callback calls the method.

  Releases all resources used by the current instance of and signals when the timer has been disposed of.

  true if the function succeeds; otherwise, false.

  The to be signaled when the Timer has been disposed of. 0
0