Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

The Multimedia Timer for the .NET Framework

Rate me:
Please Sign up or sign in to vote.
4.89/5 (66 votes)
1 Mar 20063 min read 392.4K   20.2K   90   103
A .NET class encapsulating the Win32 multimedia timer.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.

After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.

Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.

Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.

Besides programming, his other interests are photography and playing his Les Paul guitars.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 149480153-Nov-20 11:59
Member 149480153-Nov-20 11:59 
QuestionHow multimedia timer handles for long work? Pin
Susan9915-Jan-19 11:51
Susan9915-Jan-19 11:51 
SuggestionDo you think updating this to use SynchronizationContext might be a good idea? Pin
Rory12345613-Jan-18 9:41
Rory12345613-Jan-18 9:41 
PraiseWorks perfectly! Pin
Magnus Krook3-Jan-18 23:12
Magnus Krook3-Jan-18 23:12 
QuestionException: Unable to start multimedia Timer Pin
Member 25692926-Oct-16 22:32
Member 25692926-Oct-16 22:32 
QuestionCross platform Timer Pin
mikhaico30-Jun-15 9:49
mikhaico30-Jun-15 9:49 
QuestionI get "The calling thread cannot access this object because a different thread owns it." Error Pin
Junaid Qadir B23-Jul-14 13:21
Junaid Qadir B23-Jul-14 13:21 
AnswerRe: I get "The calling thread cannot access this object because a different thread owns it." Error Pin
Member 807229210-Aug-14 0:27
Member 807229210-Aug-14 0:27 
QuestionMultimediatimer and USB HID Communication Pin
OzkanKafadar9-Jul-14 0:45
OzkanKafadar9-Jul-14 0:45 
Questionvery useful, easy implementation. Thank you! Pin
Member 1077314525-Apr-14 7:19
Member 1077314525-Apr-14 7:19 
QuestionError with OneShot Timer : timer is still 'running' after Tick Pin
Member 1069630128-Mar-14 9:55
Member 1069630128-Mar-14 9:55 
GeneralMy vote of 5 Pin
chris wedgwood22-Aug-13 3:58
chris wedgwood22-Aug-13 3:58 
GeneralMy vote of 5 Pin
CrunchyDoodle19-Jun-13 7:08
CrunchyDoodle19-Jun-13 7:08 
QuestionExcellent timer Pin
Jayawant Karale30-Apr-13 1:41
Jayawant Karale30-Apr-13 1:41 
GeneralMy vote of 5 Pin
Dale Barnard18-Apr-13 10:21
Dale Barnard18-Apr-13 10:21 
QuestionObsolete Pin
jfriedman12-Apr-13 18:13
jfriedman12-Apr-13 18:13 
AnswerRe: Obsolete Pin
Dylan Renwick26-Nov-13 1:31
Dylan Renwick26-Nov-13 1:31 
GeneralRe: Obsolete Pin
jfriedman26-Nov-13 4:00
jfriedman26-Nov-13 4:00 
GeneralRe: Obsolete Pin
Dylan Renwick26-Nov-13 4:12
Dylan Renwick26-Nov-13 4:12 
GeneralRe: Obsolete Pin
jfriedman26-Nov-13 6:47
jfriedman26-Nov-13 6:47 
GeneralGreat Timer Pin
Christopher Brooks19-Feb-13 10:49
Christopher Brooks19-Feb-13 10:49 
QuestionSystematic Timer Delay Pin
diggy doggit11-Feb-13 20:21
diggy doggit11-Feb-13 20:21 
QuestionThread kept alive after dispose Pin
Lee Reid10-Jul-12 3:26
Lee Reid10-Jul-12 3:26 
QuestionTimer Pin
Member 869768715-May-12 12:44
Member 869768715-May-12 12:44 
QuestionRunning into timer drift Pin
BHort15-Sep-11 10:29
BHort15-Sep-11 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.