Export (0) Print
Expand All

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

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

  2. Wire the event handler, myTimer_Elapsed, to the Elapsed event. For more information, see Consuming Events. Your code should look like this:

    		myTimer.Elapsed +=
    		   new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
    
    NoteNote:

    For more information on this syntax, see Creating Event Handlers in Windows Forms.

  3. Create the myTimer_Elapsed procedure and define the code you want to process the event.

    	private void myTimer_Elapsed(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 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!");
    	}
    

Community Additions

ADD
Show:
© 2014 Microsoft