cTimers class
A cTimers is a numbered group of cTimer objects that share one event. Each timer has its own interval and is switched on and off on its own, but all of them raise the single Timer event of the group, with the number of the timer. The code is by David Zimmer (2003).
Dim Group As New cTimers
Debug.Print Group.Add() & " " & Group.Add() & " " & Group.Count
Group(2).Interval = 250
Debug.Print Group(2).Interval & " " & Group(1).Interval
Group.BulkLoad 3
Debug.Print Group.Count
' Output:
' 1 2 2
' 250 2000
' 6
Properties
Count
An Integer, the number of timers in the group.
Item
Returns one timer.
Syntax: object.Item ( Index ) As cTimer
- Index
- required An Integer, from 1 to Count.
It is the default member, so Group(2) is Group.Item(2). A number above Count raises error -2147221504 (vbObjectError), with the text Index does not Exist, and 0 or a negative number raises error 9.
Methods
Add
Adds a timer to the group.
Syntax: object.Add ( ) As Integer
Returns the number of the new timer, counted from 1. The timer is off, with an Interval of 2000; start it with Group(n).Enabled = True.
BulkLoad
Adds several timers at once.
Syntax: object.BulkLoad ( num ) As Variant
- num
- required An Integer from 1 to 199.
Adds num + 1 timers, not num: a call with 3 adds 4. Any other value raises error -2147221504 with the text No more than 200 times allowed per class (the text means timers). The function returns nothing useful.
Events
Timer
Raised when the interval of one of the enabled timers has passed.
Syntax: Event object_Timer ( Index )
- Index
- An Integer, the number of the timer that is raising the event.
A handler is written for a variable declared with WithEvents. The timers of the group do not raise their own Timer events.
Private WithEvents Group As cTimers
Private Sub StartTimers()
Set Group = New cTimers
Group.BulkLoad 2
Group(1).Interval = 500
Group(1).Enabled = True
Group(2).Interval = 1500
Group(2).Enabled = True
End Sub
Private Sub Group_Timer(ByVal Index As Integer)
Debug.Print "timer " & Index
End Sub
Note
A cTimers registers itself in a collection of the package’s internal module mTimer, which keeps a reference to it. The group is therefore not destroyed when the program releases its last reference, and its enabled timers go on running. Set Enabled of each timer to False first.