Timer Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Provides a mechanism for executing a method at specified intervals. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
The Timer type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Timer(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. |
![]() | Timer(TimerCallback, Object, Int32, Int32) | Initializes a new instance of the Timer class, using a 32-bit signed integer to specify the time interval. |
![]() | Timer(TimerCallback, Object, Int64, Int64) | Initializes a new instance of the Timer class, using 64-bit signed integers to measure time intervals. |
![]() | Timer(TimerCallback, Object, TimeSpan, TimeSpan) | Initializes a new instance of the Timer class, using TimeSpan values to measure time intervals. |
![]() | Timer(TimerCallback, Object, UInt32, UInt32) | Initializes a new instance of the Timer class, using 32-bit unsigned integers to measure time intervals. |
| Name | Description | |
|---|---|---|
![]() | Change(Int32, Int32) | Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals. |
![]() | Change(Int64, Int64) | Changes the start time and the interval between method invocations for a timer, using 64-bit signed integers to measure time intervals. |
![]() | Change(TimeSpan, TimeSpan) | Changes the start time and the interval between method invocations for a timer, using TimeSpan values to measure time intervals. |
![]() | Change(UInt32, UInt32) | Changes the start time and the interval between method invocations for a timer, using 32-bit unsigned integers to measure time intervals. |
![]() | Dispose() | Releases all resources used by the current instance of Timer. |
![]() | Dispose(WaitHandle) | Releases all resources used by the current instance of Timer and signals when the timer has been disposed of. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
Use a TimerCallback delegate to specify the method you want the 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 ThreadPool thread that is 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 by using the Change method.
Note: |
|---|
As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected. |
When a timer is no longer needed, use the Dispose method to free the resources held by the timer. To receive a signal when the timer is disposed, use the Dispose(WaitHandle) method overload that takes a WaitHandle. The WaitHandle is signaled when the timer has been disposed.
The callback method executed by the timer should be reentrant, because it is called on 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 scenarios in which the user interface must be updated, because its callbacks do not occur on the user interface thread. System.Windows.Threading::DispatcherTimer is a better choice in those scenarios, because its events are raised on the user interface thread. |
The following example demonstrates the features of the Timer class. This example creates a timer, uses the Change method to change its interval, and then uses the 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.
Note: |
|---|
For information on how to compile and run this example code, see Building examples that have static TextBlock controls for Windows Phone 8. |


Note: