Introduction
The Win32 multimedia timer services provide the greatest degree of timing accuracy. They allow you to schedule timing events at a higher resolution than other timer services. This can be useful in a multimedia application where timing accuracy is of utmost importance. For example, a MIDI application needs timing events that are as finely grained as possible.
Unfortunately, the Win32 multimedia timer is not part of the .NET Framework. However, by using the .NET interoperability services, the multimedia timer can be brought into the .NET fold.
The multimedia timer class
The multimedia Timer
class encapsulates several of the Win32 multimedia functions to make using the multimedia timer in the .NET environment easy and painless. The Timer
class has Start
and Stop
methods for starting and stopping the Timer
respectively. It also implements the IComponent
interface so that it can be dragged and dropped onto the Windows Designer. When the Timer
Period
has elapsed, it raises a Tick
event. All are very simple and straightforward. In addition, the class has several properties:
Capabilities
� Gets a structure representing the multimedia timer capabilities.
Mode
� Gets or sets the timer mode.
Period
� Gets or sets the time between timer events in milliseconds.
Resolution
� Gets or sets the timer resolution in milliseconds.
SynchronizingObject
- Gets or sets the ISynchronizeInvoke
object the timer is using for marshaling events.
The Capabilities
property is a static
property that gets a TimerCaps
structure representing your system's multimedia timer's minimum and maximum Period
values. Each multimedia Timer
you create has the same capabilities. Note, I have not been able to find hard documentation for this, but it appears that the maximum number of multimedia timers you can create on Windows XP is 16. I assume this is true for other versions of Windows as well, but I don't know.
The Mode
property gets or sets the Timer
's firing mode. If the Mode
is set to TimerMode.OneShot
, the Timer
will fire only once after the Period
value has elapsed. Otherwise, if it is set to TimerMode.Periodic
, it will fire continuously each time the Period
value has elapsed.
The Period
property gets or sets the time in milliseconds between each Tick
event.
The Resolution
property gets or sets the Timer
's accuracy. The lower the value of this property, with zero being the lowest, the higher the accuracy. However, the documentation for the Win32 multimedia timer warns that "To reduce system overhead, however, you should use the maximum value appropriate for your application".
The SynchronizingObject
property gets or sets the timer's ISynchronizeInvoke
object. Initially, this property is null
. When you initialize to an ISynchronizeInvoke
object, the timer will marshal the events it generates to the same thread in which the ISynchronizeInvoke
object is running. For example, if you are using the timer in a Windows Form
, you can initialize the timer's SynchronizingObject
to the Form
itself. The timer will then marshal its events to the Form
's thread.
Conclusion
I hope you find this class useful. My hope is that it will find its way into .NET multimedia applications. Comments and suggestions are most welcome.
History
- 11/22/2003 - Article submitted.
- 10/24/2005 - Major article revision, source code rewritten and updated.
- 03/01/2006 - Article revision, source code rewritten and updated.