Assembly: System (in system.dll)
'Declaration Public Class Timer Inherits Component Implements ISupportInitialize 'Usage Dim instance As Timer
The Timer 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 Timer 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 Timer 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. For more information on server-based timers, see Introduction to Server-Based Timers.
The Timer 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 Timer to start the batch processing every 30 minutes.
When AutoReset is set to false, the Timer raises the Elapsed event only once, after the first Interval has elapsed. To keep raising the Elapsed event on the Interval, set AutoReset to true.
In the .NET Framework version 2.0 and earlier, the Timer 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.
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. Thus, 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.
If you use the Timer with a user interface element, such as a form or control, assign the form or control that contains the Timer to the SynchronizingObject property, so that the event is marshaled to the user interface thread.
The Timer is not visible at run time.
For a list of initial property values for an instance of Timer, see the Timer constructor.
The following code example sets up an event handler for the Timer.Elapsed event, creates a timer, and starts the timer. The event handler displays the SignalTime property each time it is raised.
Imports System Imports System.Timers Public Class Timer1 Public Shared Sub Main() ' Normally, the timer is declared at the class level, so ' that it doesn't go out of scope when the method ends. ' In this example, the timer is needed only while Main ' is executing. However, KeepAlive must be used at the ' end of Main, to prevent the JIT compiler from allowing ' aggressive garbage collection to occur before Main ' ends. Dim aTimer As New System.Timers.Timer() ' Hook up the Elapsed event for the timer. AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000 aTimer.Enabled = True Console.WriteLine("Press the Enter key to exit the program.") Console.ReadLine() ' Keep the timer alive until the end of Main. GC.KeepAlive(aTimer) End Sub ' Specify what you want to happen when the Elapsed event is ' raised. Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) Console.WriteLine("Hello World!") End Sub End Class
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.