TTimer
Fonte: source/classes/timer.prg
Standalone class
TTimer wraps the Win32 SetTimer/KillTimer API to fire a Harbour code block at configurable intervals. Timers are used for periodic background tasks, animations, auto-save, and polling.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
nInterval | Numeric | Timer interval in milliseconds (default 18) |
bAction | Block | Code block executed on each timer tick |
lActive | Logical | Whether the timer is currently running |
nId | Numeric | Unique timer identifier |
Methods
| Method | Description |
|---|---|
New( nInterval, bAction, oWnd ) | Create a timer. Default interval 18 ms. If oWnd is omitted, the active window is used. |
Activate() | Start the timer by calling SetTimer() |
DeActivate() | Stop the timer by calling KillTimer() |
End() | Deactivate and remove the timer from the global list |
Commands
DEFINE TIMER oTmr INTERVAL nMs ACTION bAction OF oWnd
Example: One-Second Timer
#include "FiveWin.ch"
function Main()
local oWnd, oTimer, nSec := 0
DEFINE WINDOW oWnd TITLE "TTimer Demo" SIZE 300, 200
@ 20, 20 SAY "Elapsed: " + Str( nSec ) + " seconds"
DEFINE TIMER oTimer INTERVAL 1000 ;
ACTION ( nSec++, oWnd:Refresh() ) ;
OF oWnd
oTimer:Activate()
ACTIVATE WINDOW oWnd CENTERED
return nil
Notes
- Timers are stored in an internal static array and matched by
nIdin the globalTimerEvent()function. - Always call
End()when a timer is no longer needed. - Keep
bActionlightweight; long operations cause UI lag.