Generates recurring events in an application.
Assembly: System (in System.dll)
Thetype exposes the following members.
|AutoReset||Gets or sets a value indicating whether the should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses.|
|CanRaiseEvents||Gets a value indicating whether the component can raise an event. (Inherited from Component.)|
|Container||Gets the IContainer that contains the Component. (Inherited from Component.)|
|DesignMode||Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)|
|Enabled||Gets or sets a value indicating whether the should raise the Elapsed event.|
|Events||Gets the list of event handlers that are attached to this Component. (Inherited from Component.)|
|Interval||Gets or sets the interval at which to raise the Elapsed event.|
|Site||Infrastructure. Gets or sets the site that binds the to its container in design mode. (Overrides Component.Site.)|
|SynchronizingObject||Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed.|
|BeginInit||Begins the run-time initialization of a that is used on a form or by another component.|
|Close||Releases the resources used by the .|
|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.)|
|Dispose()||Releases all resources used by the Component. (Inherited from Component.)|
|Dispose(Boolean)||Infrastructure. Releases all resources used by the current . (Overrides Component.Dispose(Boolean).)|
|EndInit||Ends the run-time initialization of a that is used on a form or by another component.|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|Finalize||Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetLifetimeService||Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|GetService||Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|InitializeLifetimeService||Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|MemberwiseClone()||Creates a shallow copy of the current Object. (Inherited from Object.)|
|MemberwiseClone(Boolean)||Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)|
|Start||Starts raising the Elapsed event by setting Enabled to true.|
|Stop||Stops raising the Elapsed event by setting Enabled to false.|
|ToString||Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)|
The component is a server-based timer, which allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing. For example, suppose you have a critical server that must be kept running 24 hours a day, 7 days a week. You could create a service that uses a to periodically check the server and ensure that the system is up and running. If the system is not responding, the service could attempt to restart the server or notify an administrator.
The server-based is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.
The component raises the Elapsed event, based on the value of the Interval property. You can handle this event to perform the processing you need. For example, suppose that you have an online sales application that continuously posts sales orders to a database. The service that compiles the instructions for shipping operates on a batch of orders rather than processing each order individually. You could use a to start the batch processing every 30 minutes.
In the .NET Framework version 2.0 and earlier, the component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.
If the SynchronizingObject property is a null reference (Nothing in Visual Basic), the Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In this situation, the event handler should be reentrant.
The event-handling method might run on one thread at the same time that another thread calls the Stop method or sets the Enabled property to false. This might result in the Elapsed event being raised after the timer is stopped. The example code for the Stop method shows one way to avoid this race condition.
Even if SynchronizingObject is not a null reference (Nothing in Visual Basic), Elapsed events can occur after the Dispose or Stop method has been called or after the Enabled property has been set to false, because the signal to raise the Elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the Elapsed event to ignore subsequent events.
If you use the with a user interface element, such as a form or control, without placing the timer on that user interface element, assign the form or control that contains the to the SynchronizingObject property, so that the event is marshaled to the user interface thread.
The is not visible at run time.
For a list of initial property values for an instance of , see the Timer constructor.
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.
The code contains declarations of the timer variable at the class level and inside Main. To see how aggressive garbage collection can affect a timer that is declared inside a long-running method, you can comment out the class-level declaration and uncomment the local variable. To keep the timer from being collected, uncomment the GC.KeepAlive method at the end of Main.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.