MSDN Library

Using Timers to Perform Regular Actions

In this lesson, you will learn how to use the Timer component to perform actions not prompted by user input.

At times, you may find it useful to repeatedly perform actions in your programs—for example, saving a file every few minutes, or updating the user interface. The Timer component enables you to perform set actions at set intervals without any input from the user.

The Timer component differs from the controls that you have used to this point, in that it does not have a visual representation at run time. Controls that have no visual representation are known as components. Because there is no way for the user to interact directly with the Timer component, it runs in the background when the program runs.

The Timer component has two properties and one event that are used most often. The System.Timers.Timer.Enabled property determines whether the Timer component works. If Enabled is set to True, Timer is active. If Enabled is set to False, Timer is not active.

The System.Timers.Timer.Interval property determines the number of milliseconds between ticks of the Timer. For example, if the Interval property is set to 1000, the Timer component will raise the Tick event every 1000 milliseconds, or every second.

The Tick event is raised by the Timer component at set intervals, depending on the value of the Interval property. You can add code to a Timer.Tick event handler, and this code executes when the Tick event occurs.

By setting the Enabled and Interval properties and adding code to the Tick event handler, you can create code that executes at set intervals without the need for user action.

To use a Timer component

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type Timer, and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a Label control and a Timer component onto the form.

    The Timer component does not appear on the form itself, but in the component tray underneath the form. This is because the timer doesn't have a visual representation.

  5. Select the Timer component, and then in the Properties window, set the Enabled property to True and the Interval property to 1000.

  6. Double-click the Timer component to open the Code Editor.

  7. In the Timer1_Tick event handler, type the following code.

    Label1.Text = My.Computer.Clock.LocalTime.ToLongTimeString
  8. Press F5 to run your application.

    The text in the label is updated every second with the correct time.

In this lesson, you learned to use the Timer component to run code at set intervals. Using a Timer component, you can schedule code to run at any regular interval you choose. This is the final lesson for this section of the guided tour.

In the next lesson, you will learn how to use multiple-item controls and determine which item the user has selected.

Next Lesson: Multiple-Item Controls: Working with ListBox and ComboBox Controls

© 2015 Microsoft