Provides a mechanism for executing a method at specified intervals. This class cannot be inherited.
Inheritance Hierarchy
System.Object
System.MarshalByRefObject
System.Threading.Timer
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Syntax
C#
C++
F#
VB
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
ExternalThreading = true)]
public sealed class Timer : MarshalByRefObject,
IDisposable
The Timer type exposes the following members.
Constructors
Name Description
Public method Timer(TimerCallback) 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.
Public method Supported by the XNA Framework Supported by Portable Class Library Timer(TimerCallback, Object, Int32, Int32) Initializes a new instance of the Timer class, using a 32-bit signed integer to specify the time interval.
Public method Supported by the XNA Framework Timer(TimerCallback, Object, Int64, Int64) Initializes a new instance of the Timer class, using 64-bit signed integers to measure time intervals.
Public method Supported by the XNA Framework Supported by Portable Class Library Timer(TimerCallback, Object, TimeSpan, TimeSpan) Initializes a new instance of the Timer class, using TimeSpan values to measure time intervals.
Public method Supported by the XNA Framework Timer(TimerCallback, Object, UInt32, UInt32) Initializes a new instance of the Timer class, using 32-bit unsigned integers to measure time intervals.
Top
Methods
Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library 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.
Public method Supported by the XNA Framework 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.
Public method Supported by the XNA Framework Supported by Portable Class Library Change(TimeSpan, TimeSpan) Changes the start time and the interval between method invocations for a timer, using TimeSpan values to measure time intervals.
Public method Supported by the XNA Framework 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.
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework Supported by Portable Class Library Dispose Releases all resources used by the current instance of Timer.
Public method Dispose(WaitHandle) Releases all resources used by the current instance of Timer and signals when the timer has been disposed of.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Remarks
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 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 Change method.
Note 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. Note that callbacks can occur after the Dispose method overload has been called, because the timer queues callbacks for execution by thread pool threads. You can use the Dispose(WaitHandle) method overload to wait until all callbacks have completed.
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 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 use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
Note Note
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
Examples
The following code example demonstrates the features of the Timer class.
C#
C++
VB
using System;
using System.Threading;
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
TimerCallback tcb = statusChecker.CheckStatus;
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period.\n");
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}
class StatusChecker
{
private int invokeCount;
private int maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
invokeCount = 0;
autoEvent.Set();
}
}
}