Timer Class
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
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
'Declaration
Public NotInheritable Class Timer _
Inherits MarshalByRefObject _
Implements IDisposable
public sealed class Timer : MarshalByRefObject,
IDisposable
public ref class Timer sealed : public MarshalByRefObject,
IDisposable
[<Sealed>]
type Timer =
class
inherit MarshalByRefObject
interface IDisposable
end
public final class Timer extends MarshalByRefObject implements IDisposable
The Timer type exposes the following members.
Constructors
Name | Description | |
---|---|---|
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, TimeSpan, TimeSpan) | Initializes a new instance of the Timer class, using TimeSpan values to measure time intervals. |
Top
Methods
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(TimeSpan, TimeSpan) | Changes the start time and the interval between method invocations for a timer, using TimeSpan values to measure time intervals. | |
Dispose | Releases all resources used by the current instance of Timer. | |
Equals(Object) | Determines whether the specified Object is equal to the current Object. (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.) | |
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.
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.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.