Bagikan melalui


How to: Create 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.

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. On the Tools menu, click Choose Toolbox Items, and then click the .NET Framework Components tab.

  2. In the Choose Toolbox Items dialog box, select the Timer check box in the System.Timers namespace. The Timer icon is added to the Components tab of the Toolbox.

  3. Select the Timer icon, and drag it to the designer surface for your form or component.

  4. In the Properties window, 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.

  5. Set the Enabled property for your component instance to true.

To create a Timer component programmatically

  1. Create an instance of the Timer class.

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

  3. Set the Enabled property for your component instance to true.

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

    Dim myTimer As New System.Timers.Timer()
    myTimer.Interval = 3000
    myTimer.Enabled = True
    
           System.Timers.Timer myTimer = new System.Timers.Timer();
            myTimer.Interval = 3000;
            myTimer.Enabled = true;
    

See Also

Tasks

How to: Monitor Server-Based Timers

Concepts

Introduction to Server-Based Timers

Reference

Timer Component Overview (Windows Forms)