How to: Monitor Server-Based TimersĀ
The Timer component raises an event called Elapsed periodically, based on the interval you specify. 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.
To create a handler for the Elapsed event
Create an instance of the Timer component and set the necessary properties for it to function. For more information, see How to: Create Instances of Server-Based Timers.
Wire the event handler,
myTimer_Elapsed
, to the Elapsed event. For more information, see Consuming Events. Your code should look like this:AddHandler myTimer.Elapsed, New _ System.Timers.ElapsedEventHandler(AddressOf Me.myTimer_Elapsed)
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myTimer.add_Elapsed( new System.Timers.ElapsedEventHandler(myTimer_Elapsed));
Note For more information on this syntax, see Creating Event Handlers in Windows Forms.
Create the
myTimer_Elapsed
procedure and define the code you want to process the event.Private Sub myTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) System.Windows.Forms.MessageBox.Show("Elapsed!", _ "Timer Event Raised!") End Sub
private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { System.Windows.Forms.MessageBox.Show("Elapsed!", "Timer Event Raised!"); }
private void myTimer_Elapsed(System.Object sender, System.Timers.ElapsedEventArgs e) { System.Windows.Forms.MessageBox.Show("Elapsed!", "Timer Event Raised!"); }
The following code shows how you can create a Timer component that raises an event every five seconds and displays a message box that alerts you to the event:
Private Sub CreateTimer() Dim Timer1 As New System.Timers.Timer() Timer1.Interval = 5000 Timer1.Enabled = True AddHandler Timer1.Elapsed, _ New System.Timers.ElapsedEventHandler(AddressOf Me.Timer1_Elapsed) End Sub Private Sub Timer1_Elapsed(ByVal sender As System.Object, _ ByVal e As System.Timers.ElapsedEventArgs) System.Windows.Forms.MessageBox.Show("Elapsed!", "Timer Event Raised!") End Sub
private void CreateTimer() { System.Timers.Timer Timer1 = new System.Timers.Timer(); Timer1.Enabled = true; Timer1.Interval = 5000; Timer1.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed); } private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { System.Windows.Forms.MessageBox.Show("Elapsed!", "Timer Event Raised!"); }
private void CreateTimer() { System.Timers.Timer Timer1 = new System.Timers.Timer(); Timer1.set_Enabled(true); Timer1.set_Interval(5000); Timer1.add_Elapsed( new System.Timers.ElapsedEventHandler(Timer1_Elapsed)); } private void Timer1_Elapsed(System.Object sender, System.Timers.ElapsedEventArgs e) { System.Windows.Forms.MessageBox.Show("Elapsed!", "Timer Event Raised!"); }
See Also
Tasks
How to: Create Instances of Server-Based Timers
Reference
Timer Component Overview (Windows Forms)
Concepts
Introduction to Server-Based Timers