Freigeben über


Performing Specific Actions at Given Intervals

The Timer control makes it possible for you to perform actions or check values at specific intervals.

Using the Timer Control

Timer controls respond to the passage of time independent of user interaction, so you can program them to take actions at regular intervals. A typical use is checking the system clock to see if it is time to do a particular task. Timers are also useful for other kinds of background processing.

To see examples of using timers

  1. Run Solution.app in the Visual FoxPro \Samples\Solution directory.
  2. In the tree view, click Controls, and then click Timer.

Each timer has an Interval property, which specifies the number of milliseconds that pass between one timer event and the next. Unless disabled, a timer continues to receive an event (appropriately named the Timer event) at roughly equal intervals of time. The Interval property has a few limitations to consider when you're programming a timer:

  • The interval can be between 0 and 2,147,483,647, inclusive, which means that the longest interval is about 596.5 hours (over 24 days).
  • The interval is not guaranteed to elapse exactly on time. To ensure accuracy, the timer should check the system clock when it needs to, rather than try to keep track of accumulated time internally.
  • The system generates 18 clock ticks per second, so even though the Interval property is measured in milliseconds, the true precision of an interval is no more than one-eighteenth of a second.
  • If your application or another application is making heavy demands on the system — such as long loops, intensive calculations, or disk, network, or port access — your application might not get timer events as often as the Interval property specifies.

Placing a Timer Control on a Form

Placing a Timer control on a form is like drawing any other control: you choose the timer tool on the Form Controls toolbar and click and drag on the form.

A Timer control

The timer appears on the form at design time so you can select it, view its properties, and write an event procedure for it. At run time, a timer is invisible and its position and size are irrelevant.

Initializing a Timer Control

A Timer control has two key properties.

Property Setting
Enabled If you want the timer to start working as soon as the form loads, set to true (.T.). Otherwise, leave this property set to false (.F.). You can choose to have an outside event (such as a click of a command button) start operation of the timer.
Interval Number of milliseconds between timer events.

Note that the Enabled property for the timer is different than for other objects. With most objects, the Enabled property determines whether the object can respond to an event caused by the user. With the Timer control, setting Enabled to false (.F.) suspends timer operation.

Remember that the Timer event is periodic. The Interval property doesn't determine "how long" as much as it determines "how often." The length of the interval should depend on how much precision you want. Because there is some built-in potential for error, make the interval one-half the desired amount of precision.

Note   The more often a timer event is generated, the more processor time is consumed in responding to the event. This can slow down overall performance. Don't set a particularly small interval unless you need it.

Responding to the Timer Event

When a Timer control's interval elapses, Visual FoxPro generates the Timer event. Typically, you respond to this event by checking some general condition, such as the system clock.

A digital clock is a very simple but highly useful application involving a Timer control. When you understand how the application works, you can enhance it to work as an alarm clock, stopwatch, or other timing device.

The digital clock application includes a timer and a label with a border. At design time, the application looks like this:

The digital clock application

At run time, the timer is invisible.

Control Property Setting
lblTime Caption  
Timer1 Interval 500 (half a second)
Timer1 Enabled True

The sole procedure in the application is the Timer event procedure:

IF THISFORM.lblTime.Caption != Time()
   THISFORM.lblTime.Caption = Time()
ENDIF

The Interval property for the timer is set to 500, following the rule of setting the Interval to half of the shortest period you want to distinguish (one second in this case). This might cause the timer code to update the label with the same time twice in one second. This could cause some visible flicker, so the code tests to see if the time is different from what is displayed in the label before it changes the caption.

See Also

Performing Specific Actions at Given Intervals | Displaying Information | Using Controls | Enhancing Control Display | Controls and Objects