Schedule tasks with VB6's Timer control

When you want something to happen after a set interval or on a regular basis, you should consider using VB6's Timer control. Follow the steps in this tip to schedule tasks using a Timer control.

VB6's Timer control is useful in almost any situation when you want something to happen after a set interval or on a regular basis. This can help save you a lot of time when you want to schedule a task in advance.

To access the Timer control, go to the VB toolbox and look for the pocket watch icon. There are two properties you will need to set when working with the Timer control:

  • The Interval property is a value between 1 and 65,535 that specifies the control's interval in milliseconds.
  • The Enabled property is a True/False value that turns the Timer on and off.

When you actually put a Timer control on a form, it's visible but will not be seen when the program runs. Also, it's important to know that the Timer control's single event, Timer, fires each time the Timer counts down from Interval.

Follow these steps to schedule tasks using a Timer control:

  1. Set the Interval property to the desired interval.
  2. Put the code you want executed in the Timer event procedure.
  3. Set the Enabled property to True to start timing.

A Timer control will cycle repeatedly as long as its Enabled property is True. To run it just once, set Enabled to False in the Timer event procedure. You can have two or more Timer controls on a form. In fact, cascading two Timer controls is the only way to time intervals longer than about 65 seconds.

Note: In theory, the Timer control's resolution is one millisecond; however, the reality is it's not that good, so you shouldn't use it for time-critical tasks.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

Editor's Picks

Free Newsletters, In your Inbox