Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Timer

The Timer Control is a cool little device for creating events that fire at predetermined intervals. This is a great replacement for DO…WHILE and INKEY() loops, which chew up a whole bunch of processor time while counting milliseconds. The timer control is flaky and unpredictable in VFP 3.x. The Fox team has done a great job of bringing this one under control in VFP 5 and later versions.

Property

Value

Purpose

Interval

Numeric

Determines length of time (in milliseconds) between Timer events.

0

Timer is disabled.

Enabled

.T.

Timer counts down Interval milliseconds and then fires the Timer event.

.F.

Timer does nothing.

Event

Purpose

Timer

Code to be executed when event occurs.

Method

Purpose

Reset

Resets the countdown to Interval; does not change Enabled state.

The timer is a control with no visible presence at runtime, but fires its Timer Event at a regular, defined Interval. If you messed with this control in VFP 3 and got frustrated, give it another shot. We have to give Microsoft credit where credit is due, and they did a great job with Timer. In the original version of this book, which covered Visual FoxPro 3.0, Timer got credit for a lot of bugs—even the dreaded double-bug icon—but in later versions, it appears vastly improved.

Timers fire when the menu is dropped down. They didn’t in VFP 3.0. Timers fire in toolbars. Timers fire when a form is being dragged around. Timers fire when another application window is resized, or when Fox’s window is resized. Timers fire when your application displays a message box. Awesome!

Example

* RemindMe.Prg
* This is a pretty trivial example of a program that accepts
* a message and the number of seconds to wait to display that
* message. In the meantime, processing can go on as normal in
* the foreground.
* Pass the message to display, and the
* number of seconds to wait to display it.
LParameters tcMessage, tnTime

_SCREEN.AddProperty("oTimer")
_SCREEN.oTimer = CreateObject("RemindTime", ;
                              tcMessage, ;
                    tnTime * 1000)  && convert to
                                              && milliseconds
RETURN

DEFINE CLASS RemindTime AS Timer
  cMessage = ""
  nTime = 0
  Interval = 5000  && check every 5 seconds
  Procedure Init(tcMessage, tnTime)
    This.cMessage = tcMessage
    This.nTime = tnTime
  EndProc
  Procedure Timer
    This.nTime = This.nTime - This.Interval
    IF This.nTime < 0  && we've exceeded the interval
      MessageBox(This.cMessage)
      This.Enabled = .F.
      RELEASE This
    ENDIF
  EndProc
EndDefine

See Also

Interval, Timer Event, Reset Method