cTimer class
A cTimer is a timer that raises its Timer event at a set interval. It is built on the Windows function SetTimer, with no window, and does not need a form. The code is by David Zimmer (2003), who allows its use in commercial and non-commercial programs. A cTimer can be used alone, as here, or as one of the numbered timers of a cTimers.
Private WithEvents Tick As cTimer
Private Count As Long
Private Sub StartTicking()
Set Tick = New cTimer
Tick.Interval = 500
Tick.Enabled = True
End Sub
Private Sub Tick_Timer()
Count = Count + 1
If Count = 3 Then Tick.Enabled = False
End Sub
The event is raised from the message loop of the program, so it is not raised while the program is busy in a long loop without DoEvents.
Properties
Enabled
A Boolean: whether the timer is running. It is False for a new object.
Setting it to True starts the timer, or starts it again from zero when it is running. Setting it to False stops it. A start with Interval of 0 or less does nothing: Enabled stays False.
Dim T As New cTimer
Debug.Print T.Interval & " " & T.Enabled
T.Interval = 0
T.Enabled = True
Debug.Print T.Enabled
T.Interval = 1000
T.Enabled = True
Debug.Print T.Enabled
T.Enabled = False
Debug.Print T.Enabled
' Output:
' 2000 False
' False
' True
' False
Interval
A Long, the time between two events in milliseconds. It is 2000 for a new object. It is an ordinary field: a new value does not reach a running timer until the timer is started again with Reset, Enable or EnableTimer.
Tag
A Variant for the program’s own use. The class does not read it.
Methods
Disable
Stops the timer.
Syntax: object.Disable
The same as setting Enabled to False.
Enable
Starts the timer, with the current Interval. A running timer is stopped and started again.
Syntax: object.Enable
EnableTimer
Sets the interval and starts the timer.
Syntax: object.EnableTimer ( mInterval )
- mInterval
- required A Long, the new Interval in milliseconds.
Reset
Stops the timer and starts it again, so that the next event comes after one full interval.
Syntax: object.Reset
The timer runs after the call, also when it was not running before it.
Events
Timer
Raised each time the interval has passed.
Syntax: Event object_Timer ( )
A handler is written for a variable declared with WithEvents. A timer that belongs to a cTimers raises the Timer event of that object instead, with its number, and not its own.
The registry of timers
The class keeps every running timer in a collection of the module mTimer, by the number that Windows gives the timer, and the callback that Windows calls, mTimer.TimerProc, finds the object there and raises its event. The module belongs to the package’s internal code and has no page; a program does not call it.
Note
The collection holds a reference to each running timer. A cTimer that is enabled is therefore not destroyed when the program releases its last reference: it stays registered and goes on running until Enabled is set to False. A timer that was never enabled is destroyed as usual, and the timer of a destroyed object is stopped.
See Also
- cTimers class – a numbered group of these timers
- cTimeuse class – a stopwatch
- TwinTimerLib package – another timer class
- TBMANLIB package – overview