this code has been superceded by the version at the new site.
High Resolution Multimedia Timer
Multimedia timers are implemented using the Win32 multimedia library (winmm.dll). Before creating a timer, it is important to be aware of a number of system limitations and VB problems which can occur when using multimedia timers.
The first thing to do with this sort of component is to ensure the code can be isolated from the VB IDE by coding it into an ActiveX DLL. You really don't want to have to try to deal with the IDE locking up every time you set a break-point.
Once it is in the DLL then the next thing is to consider how to reduce the number of a timer instances to ensure you aren't too likely to run out of them. In this code, the timer itself is implemented in a module. If you implement code in a module, this module will be global to all classes which use it within a process. As well as your executable project, this includes any OCX or DLL which attaches to the DLL. Therefore it is possible to create a single timer to service as many classes as you want. The trick is to ensure that the module doesn't get a reference to the classes that are attached, otherwise you will get a circular reference. You can prevent circular references by storing object pointers rather than direct references to classes as described in Subclassing without the crashes.
The final problem to work around is that you can only call a very limited selection of functions during a multimedia timer event. I work around this by using the PostMessage function. The function posts a custom WM_USER message to a hidden form owned by the timer module within the DLL. The form is subclassed by the DLL so when the custom message is received it is intercepted by the hidden form's WindowProc function, and this allows the module to forward the message on as an event to any objects which are using the DLL.
This method of working may seem rather stupid - a timer event is received by the DLL's timer module, then posted to a form and intercepted from the form using the same DLL - but it turns out this is vital to ensure the timer is stable during operation. There are various examples of how to crash your system by not doing this available elsewhere on the Internet (no names - you know who you are :)
When using the DLL provided with the download, you can get stable crash-free operation. You can press stop in the VB IDE when the timer is running without any problems. Even if you set the timer to do something at a silly rate which your code can't keep up with you shouldn't have difficulty provided you use the Event interface, because the PostMessage operation prevents things going wrong.
However, I should point out if you are running the DLL code in the group project, no such niceties apply. No point giving disclaimers about when and how you will crash - you will! It isn't a problem though, just restart VB. If you're interested in building this code into your project, I strongly advise using the DLL to begin with and then only incorporate the code when you come to make the EXE.
Using the HiRes Timer
The HiRes Timer offers a simple way of adding and removing timers, and there are two ways of receiving timer events (the Third Way, as proposed by UK's increasingly bizarre Prime Minister, will not be made available here).
If you aren't interested in the code for this sort of thing, then I recommend looking at the High-Resolution timer available from the Common Controls Replacement Project (CCRP). This offers extra interfaces such as a countdown timer and an About screen (Hmmm.)
Top code, but depends on your aim. If you want to create a MIDI or Audio sequencer, then you need the code, and CCRP can't give you that for their own personal reasons. An audio project has got to to be 1ms fast all the time and you don't want to know about the hacks that some other coder went through to get their version to happen - you need to make your own hacks and get it to run at the right speed. Hence the source code here.
One odd thing about the CCRP component is that it appears to be recommended as a general timer - I'm not sure that the Multimedia one does this job. The VB Timer is a nice thing in all ways except that it requires a form to site it on, but don't let that put you off. VB only ever uses one timer resource no matter how many apps run on the system (because all VB apps by definition are linked to the VB run-time library) so you are rarely in danger of running out of timers - a problem which you will find much more seriously if you use the Timer exposed by the Subclassing and Timer Assistant DLL - all I can say is this is Bruce McKinney's code and I'm sorry!