Export (0) Print
Expand All

Creating Instances of Server-Based Timers

You create Timer components when you want to build interval-based events into your multi-threaded application. You can add an instance of the Timer component to Web Forms, Windows Services, and component classes. If you add an instance of the Timer component to a visual designer (such as the Web Forms Designer), the component appears in a small area below the bottom border of the form. This is known as the component tray and acts as a place to display all of the non-visual items associated with the form.

There are two ways you can create an instance of the Timer component:

  • You can drag an instance of the Timer component from the Components tab of the Toolbox to a form or other designer.
  • You can create an instance of the Timer component in code.

To create a Timer component from the Toolbox

  1. Access the Components tab of the Toolbox.
  2. Select the Timer icon, and drag it to the designer surface for your form or component.
  3. Set the Interval property for your component instance to the length of time, in milliseconds, after which you want the component to raise an event.
  4. Set the Enabled property for your component instance to true.

To create a Timer component programmatically

  1. Add a reference to the System.Timers DLL and import the System.Timers namespace into your project.
  2. Create an instance of the Timer class in code.
  3. Set the Interval property for your component instance to the length of time, in milliseconds, after which you want the component to raise an event.
  4. Set the Enabled property for your component instance to true.

    The following code shows how to create and configure a Timer component programmatically.

    ' Visual Basic
    Dim myTimer As New System.Timers.Timer()
    myTimer.Enabled = True
    myTimer.Interval = 3000
    
    // C#
    System.Timers.Timer myTimer = new System.Timers.Timer();
    myTimer.Enabled = true;
    myTimer.Interval = 3000;
    

See Also

Introduction to Server-Based Timers | Monitoring Server-Based Timers | Introduction to the Windows Forms Timer Component

Show:
© 2014 Microsoft