Export (0) Print
Expand All
16 out of 39 rated this helpful - Rate this topic

Timer Class

Generates recurring events in an application.

For a list of all members of this type, see Timer Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Timers.Timer

[Visual Basic]
Public Class Timer
   Inherits Component
   Implements ISupportInitialize
[C#]
public class Timer : Component, ISupportInitialize
[C++]
public __gc class Timer : public Component, ISupportInitialize
[JScript]
public class Timer extends Component implements ISupportInitialize

Thread Safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Remarks

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.

Note   The event-handling method might be called even after the Stop method is called. The event-handling method might run on one thread at the same time that a call to the Stop method runs on another thread. This might result in the Elapsed event being raised even after the Stop method is called. To prevent this, use the SignalTime property to compare the time the event was raised to the time the Stop method was called. If the event was raised after the Stop method was called, do not process the event.

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.

Note   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.

The Timer is not visible at run time.

For a list of initial property values for an instance of Timer, see the Timer constructor.

Example

[Visual Basic, C#, C++] The following example creates a Timer that displays "Hello World!" on the console every five seconds.

[Visual Basic, C#, C++] Use the System.Timers namespace for this example.

[Visual Basic] 
Public Class Timer1
    
    Public Shared Sub Main()
        Dim aTimer As New System.Timers.Timer()
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        ' Set the Interval to 5 seconds.
        aTimer.Interval = 5000
        aTimer.Enabled = True
        
        Console.WriteLine("Press 'q' to quit the sample.")
        While Console.Read() <> CInt("q")
        End While
    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


[C#] 
public class Timer1
 {
 
     public static void Main()
     {
         System.Timers.Timer aTimer = new System.Timers.Timer();
         aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
         // Set the Interval to 5 seconds.
         aTimer.Interval=5000;
         aTimer.Enabled=true;
 
         Console.WriteLine("Press \'q\' to quit the sample.");
         while(Console.Read()!='q');
     }
 
     // Specify what you want to happen when the Elapsed event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
         Console.WriteLine("Hello World!");
     }
 }
 

[C++] 
public __gc class Timer1
{
public:
    static void Main() {
        System::Timers::Timer* aTimer = new System::Timers::Timer;
        aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer->Interval=5000;
        aTimer->Enabled=true;
    }
private:
    // Specify what you want to happen when the Elapsed event is raised.
     static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
     {
         Console::WriteLine(S"Hello World!");
     }
};

int main()
{
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Timers

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

Timer Members | System.Timers Namespace | AutoReset | Interval | Introduction to Server-Based Timers | Elapsed | Timer

Show:
© 2014 Microsoft. All rights reserved.