by selftaught (17 Submissions)
Category: Coding Standards
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating:
(6 Votes)
Now updated with some comments!
This is a simple timer module with a standard implementation class. The module uses only a single timer resource, but it can fire events to a nearly unlimited number of subscribers. The worst precision that I have gotten is just under 2 hundredths of a second. The code uses the system time to determine when to fire, so there is no uptime limitation as with GetTickCount.
Inputs
Implement the iTimer interface on any of your classes, and then call:
SetATimer Me, (Interval), (Tag)
where interval is in milliseconds and Tag is an identifier for the timer to be returned with each fire. You can set multiple timers for one object without creating any new objects.
Assumes
Uses an illegal object reference to allow the object to terminate without forcing you to have a dispose-type method. This works just fine, as long as you call KillTimers Me in the terminate event of your class.
Side Effects
This code kills the timer after each entry to TimerProc. It goes through all of the subscribers and decides when the next one should be, and sets a timer for the appropriate duration. This allows many timers with different intervals to be fired, never using more than a single timer resource.
Download Timers, simplified (5 KB)