So this one is a little bit different - it's the first code I've written in a long time that I have no use for personally, nor can I really imagine a use for it. That said, it was fun to write and it has some interesting features that I think might be useful to somebody out there, so I figured I'd share it.
As I was nodding off to sleep the other day, I got thinking about a different approach to Timers (as one often does when nodding off to sleep). The idea that came to mind was to have a single centralized timer "host" that any number of timer "clients" can register with and be notified when the appropriate interval has elapsed, instead of the usual method of having Timer controls and/or Timer classes in all your classes/forms.
The more I thought about it, the more I thought this approach would offer some interesting properties/behaviours - for example, it would be possible to easily pause/unpause all the timer clients in one call, and even selectively pause/unpause timers based on individual instances or various "groups" of instances (for example, implementing Class Type, Tag, or combinations thereof). Maybe useful for certain types of games?
So I set to work straight away...
![Name: 2025-02-01_11-22-53a.jpg
Views: 78
Size: 40.9 KB]()
And a couple days later, I had something that seems to work reasonably well and includes a fairly comprehensive demo form for testing/experimenting:
![Name: 2025-02-01_11-47-06.jpg
Views: 104
Size: 28.7 KB]()
And here is the full source + demo for anyone that is interested in trying it out:
SOURCE CODE + DEMO: TimerHostDemo2.zip
IMPORTANT NOTE: The code is very new and lightly tested, so there will likely be bugs or undesired operation under certain loads that I haven't considered yet. Please report any problems here and I will try to address them promptly.
Before You Begin
It will be useful to understand some core concepts before you begin:
Them's the basics! Hopefully between the above, the demo code, and the cdode comments you will be able to understand how it all works, but I'm happy to answer any questions you may have here.
Final Thoughts
I'd also be very interested to hear whether you think this type of Timer approach has any real-world uses, and if so, what they might be. Or is this approach a generally bad idea borne of the kind of logic you only experience at the "twilight zone" before sleep? In any case, enjoy it and use it if you can, otherwise chuck it in the bin ;)
As I was nodding off to sleep the other day, I got thinking about a different approach to Timers (as one often does when nodding off to sleep). The idea that came to mind was to have a single centralized timer "host" that any number of timer "clients" can register with and be notified when the appropriate interval has elapsed, instead of the usual method of having Timer controls and/or Timer classes in all your classes/forms.
The more I thought about it, the more I thought this approach would offer some interesting properties/behaviours - for example, it would be possible to easily pause/unpause all the timer clients in one call, and even selectively pause/unpause timers based on individual instances or various "groups" of instances (for example, implementing Class Type, Tag, or combinations thereof). Maybe useful for certain types of games?
So I set to work straight away...
And a couple days later, I had something that seems to work reasonably well and includes a fairly comprehensive demo form for testing/experimenting:
And here is the full source + demo for anyone that is interested in trying it out:
SOURCE CODE + DEMO: TimerHostDemo2.zip
IMPORTANT NOTE: The code is very new and lightly tested, so there will likely be bugs or undesired operation under certain loads that I haven't considered yet. Please report any problems here and I will try to address them promptly.
Before You Begin
It will be useful to understand some core concepts before you begin:
- A Timer Host is an instance of the cTimerHost class (provided with the demo source). It can be instantiated anywhere (Class Module, Form, Standard Module) and it includes no Events.
- A Timer Client is an instance of any class that implements the ITimerClient interface (provided with the demo source). Timer clients will be any of your classes or forms that you want to register with a Timer Host to process code at specified intervals.
- The ITimerClient interface is a very simple interface with only 2 methods: ITimerClient_TimerFired() and ITimerClient_IsTypeOfMe(). See the ITimerClient class that is included with the demo for details on how to implement these methods.
- A Timer Entry is a unique combination of Timer Client instance (identified by ObjPtr) and Tag that is registered with a Timer Host instance. So any single Timer Client instance can be registered with any number of Tags, and each combination of Timer Client instance and Tag will be a Timer Entry in the Timer Host.
- You can register any number of Timer Client instances (even from different ITimerClient implementing classes) and Tags with a single Timer Host (up to the point of system resource exhaustion anyway).
Them's the basics! Hopefully between the above, the demo code, and the cdode comments you will be able to understand how it all works, but I'm happy to answer any questions you may have here.
Final Thoughts
I'd also be very interested to hear whether you think this type of Timer approach has any real-world uses, and if so, what they might be. Or is this approach a generally bad idea borne of the kind of logic you only experience at the "twilight zone" before sleep? In any case, enjoy it and use it if you can, otherwise chuck it in the bin ;)