public sealed class Timer : MarshalByRefObject, IDisposable
|
When creating a timer, the application specifies an amount of time to wait before the first invocation of the delegate methods (due time), and an amount of time to wait between subsequent invocations (period). A timer invokes its methods when its due time elapses, and invokes its methods once per period thereafter. You can change these values, or you can disable the timer, by using the Timer.Change method.
When a timer is no longer needed, use the Timer.Dispose method to free the resources held by the timer.
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; } } }
This code produces the following output (the exact timings returned will vary):
08:02:09.4811456 Checking Status 1. 08:02:10.4825856 Checking Status 2. 08:02:11.4840256 Checking Status 3. 08:02:12.4854656 Checking Status 4. 08:02:13.4869056 Checking Status 5. changed... 08:02:23.4912912 Checking Status 6. 08:02:23.5914352 Checking Status 7. 08:02:23.6915792 Checking Status 8. 08:02:23.7917232 Checking Status 9. 08:02:23.8918672 Checking Status 10. disposing of timer... Timer example done.
ctor #1 | Overloaded:.ctor(TimerCallback callback, object state, int dueTime, int period) Initializes a new instance of the Timer class, using 32-bit signed integers to measure time intervals. |
ctor #2 | Overloaded:.ctor(TimerCallback callback, object state, long dueTime, long period) Initializes a new instance of the Timer class, using 64-bit signed integers to measure time intervals. |
ctor #3 | Overloaded:.ctor(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) Initializes a new instance of the Timer class, using TimeSpan values to measure time intervals. |
ctor #4 | Overloaded:.ctor(TimerCallback callback, object state, uint dueTime, uint period) Initializes a new instance of the Timer class, using 32-bit unsigned integers to measure time intervals. |
Change | Overloaded:Change(int dueTime, int period) Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals. |
Change | Overloaded:Change(long dueTime, long period) Changes the start time and the interval between method invocations for a timer, using 64-bit signed integers to measure time intervals. |
Change | Overloaded:Change(TimeSpan dueTime, TimeSpan period) Changes the start time and the interval between method invocations for a timer, using TimeSpan values to measure time intervals. |
Change | Overloaded:Change(uint dueTime, uint period) Changes the start time and the interval between method invocations for a timer, using 32-bit unsigned integers to measure time intervals. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
Dispose | Overloaded:Dispose() Releases all resources used by the current instance of Timer. |
Dispose | Overloaded:Dispose(WaitHandle notifyObject) Releases all resources used by the current instance of Timer and signals when the timer has been disposed of. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize | Overridden: Releases the resources held by the current instance. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public Timer(Timer( |
callback
state
dueTime
period
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The dueTime or period parameter is negative and is not equal to Timeout.Infinite. |
ArgumentNullException | The callback parameter is null. |
If dueTime is zero (0), callback performs its first invocation immediately. If dueTime is Timeout.Infinite, callback does not invoke its methods. The timer is disabled, but it can be re-enabled using the Timer.Change method.
If period is zero (0) or Timeout.Infinite and dueTime is not Infinite, callback invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled using the Change method.
public Timer(Timer( |
callback
state
dueTime
period
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The dueTime or period parameter is negative and is not equal to Timeout.Infinite. |
NotSupportedException | The dueTime or period parameter is greater than 4294967294. |
If dueTime is zero (0), callback performs its first invocation immediately. If dueTime is Timeout.Infinite, callback does not invoke its methods. The timer is disabled, but it can be re-enabled using the Timer.Change method.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, callback invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled using the Change method.
public Timer(Timer( |
callback
state
dueTime
period
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The number of milliseconds in the value of dueTime or period is negative and not equal to Timeout.Infinite, or is greater than Int32.MaxValue. |
ArgumentNullException | The callback parameter is null. |
If dueTime is zero (0), callback performs its first invocation immediately. If dueTime is Timeout.Infinite, callback does not invoke its methods. The timer is disabled, but it can be re-enabled using the Timer.Change method.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, callback invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled using the Change method.
[CLSCompliant(false)] |
callback
state
dueTime
period
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The dueTime or period parameter is negative and is not equal to Timeout.Infinite. |
ArgumentNullException | The callback parameter is null. |
If dueTime is zero (0), callback performs its first invocation immediately. If dueTime is Timeout.Infinite, callback does not invoke its methods. The timer is disabled, but it can be re-enabled using the Timer.Change method.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, callback invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled using the Change method.
dueTime
period
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The dueTime or period parameter is negative and is not equal to Timeout.Infinite. |
If dueTime is zero (0), the delegate specified at Timer construction time performs its next invocation immediately. If dueTime is Timeout.Infinite, no methods are invoked. The timer is disabled, but it can be re-enabled by calling this method and specifying a positive value for dueTime.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, the delegate specified at Timer construction time invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled by specifying a positive value for period.
dueTime
period
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The dueTime or period parameter is less than -1. |
NotSupportedException | The dueTime or period parameter is greater than 4294967294. |
If dueTime is zero (0), the delegate specified at Timer construction time performs its next invocation immediately. If dueTime is Timeout.Infinite, no methods are invoked. The timer is disabled, but it can be re-enabled by calling this method and specifying a positive value for dueTime.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, the delegate specified at Timer construction time invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled by specifying a positive value for period.
dueTime
period
If dueTime is zero (0), the delegate specified at Timer construction time performs its next invocation immediately. If dueTime is Timeout.Infinite, no methods are invoked. The timer is disabled, but it can be re-enabled by calling this method and specifying a positive value for dueTime.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, the delegate specified at Timer construction time invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled by specifying a positive value for period.
dueTime
period
If dueTime is zero (0), the delegate specified at Timer construction time performs its next invocation immediately. If dueTime is Timeout.Infinite, no methods are invoked. The timer is disabled, but it can be re-enabled by calling this method and specifying a positive value for dueTime.
If period is zero (0) or Timeout.Infinite, and dueTime is not Infinite, the delegate specified at Timer construction time invokes its methods once. The periodic behavior of the timer is disabled, but it can be re-enabled by specifying a positive value for period.
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public void Dispose(); |
public bool Dispose( |
notifyObject
Exception Type | Condition |
---|---|
NullReferenceException | The notifyObject parameter is null. |
When this method completes, it signals the WaitHandle specified by the notifyObject parameter. This method then calls GC.SuppressFinalize to prevent the garbage collector from calling the Finalize method.
~Timer(); |
This method overrides System.Object.Finalize.
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
public virtual string ToString(); |