Generates an event after a set interval, with an option to generate recurring events.
To browse the .NET Framework source code for this type, see the Reference Source.
Assembly: System (in System.dll)
Gets or sets a Boolean indicating whether the Elapsed event only once (false) or repeatedly (true).should raise the
Gets a value indicating whether the component can raise an event.(Inherited from Component.)
Gets or sets a value indicating whether the Elapsed event.should raise the
Gets or sets the interval, expressed in milliseconds, at which to raise the Elapsed event.
This API supports the product infrastructure and is not intended to be used directly from your code. Gets or sets the site that binds the Component::Site.)to its container in design mode.(Overrides
Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed.
Begins the run-time initialization of athat is used on a form or by another component.
Releases the resources used by the.
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)
This API supports the product infrastructure and is not intended to be used directly from your code. Releases all resources used by the current Component::Dispose(Boolean).).(Overrides
Ends the run-time initialization of athat is used on a form or by another component.
Determines whether the specified object is equal to the current object.(Inherited from Object.)
Serves as the default hash function. (Inherited from Object.)
Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)
Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)
To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.
The Elapsed event in your application after the number of milliseconds in the Interval property has elapsed. You can configure the object to raise the event just once or repeatedly using the AutoReset property. Typically, a object is declared at the class level so that it stays in scope as long as it is needed. You can then handle its Elapsed 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 object 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.component is a server-based timer that raises an
The System.Threading::Timer class instead.class is available in the .NET Framework only. It is not included in the .NET Standard Library and is not available on other platforms, such as .NET Core or the Universal Windows Platform. On these platforms, as well as for portability across all .NET platforms, you should use the
This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.
The server-based Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.class is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised
The Elapsed event, based on the value (in milliseconds) 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.component raises the
The System.Timers.Timer class has the same resolution as the system clock. This means that the Elapsed event will fire at an interval defined by the resolution of the system clock if the Interval property is less than the resolution of the system clock. For more information, see the Interval property.
When AutoReset is set to false, a object raises the Elapsed event only once, after the first Interval has elapsed. To keep raising the Elapsed event regularly at the interval defined by the Interval, set AutoReset to true, which is the default value.
The Elapsed event. This behavior is subject to change in future releases of the .NET Framework. Note, however, that this is not true of event handlers that execute asynchronously and include the await operator (in C#) or the Await operator (in Visual Basic). Exceptions thrown in these event handlers are propagated back to the calling thread, as the following example illustrates. For more information on exceptions thrown in asynchronous methods, see Exception Handling (Task Parallel Library).component catches and suppresses all exceptions thrown by event handlers for the
If the SynchronizingObject property is null, 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 null, 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 SynchronizingObject property, so that the event is marshaled to the user interface thread.class 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
For a list of default property values for an instance of Timer constructor., see the
Be aware that the .NET Framework Class Library includes four classes named Timer, each of which offers different functionality:
The following example instantiates a System.Timers.Timer object that fires its Timer::Elapsed event every two seconds (2,000 milliseconds), sets up an event handler for the event, and starts the timer. The event handler displays the value of the ElapsedEventArgs::SignalTime property each time it is raised.
Available since 1.1
Any public static members of this type are thread safe. Any instance members are not guaranteed to be thread safe.