Click here to Skip to main content
Click here to Skip to main content

The Multimedia Timer for the .NET Framework

By , 1 Mar 2006
 

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

About the Author

Leslie Sanford
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionExcellent timermemberJayawant Karale30 Apr '13 - 1:41 
GeneralMy vote of 5memberDale Barnard18 Apr '13 - 10:21 
QuestionObsoletememberjfriedman12 Apr '13 - 18:13 
GeneralGreat TimermemberChristopher Brooks19 Feb '13 - 10:49 
QuestionSystematic Timer Delaymemberdiggy doggit11 Feb '13 - 20:21 
QuestionThread kept alive after disposememberLee Reid10 Jul '12 - 3:26 
QuestionTimermemberMember 869768715 May '12 - 12:44 
QuestionRunning into timer driftmemberBHort15 Sep '11 - 10:29 
AnswerRe: Running into timer driftmemberroadz118 Mar '12 - 11:13 
GeneralWith multiple timers, MultimediaTimer does not always re-start properlymemberzamirkhan4 May '11 - 10:48 
GeneralRe: With multiple timers, MultimediaTimer does not always re-start properlygroupSparkling_ouc29 Jun '11 - 22:03 
GeneralI get a Timer error sometimes [modified]memberD-Flux31 Mar '11 - 0:17 
GeneralMultimedia timermemberJSamedov9 Feb '11 - 22:14 
GeneralRe: Multimedia timermemberzamirkhan4 May '11 - 10:21 
GeneralThanks very muchmemberMat_Davies9 Dec '10 - 7:30 
QuestionCan I start up several instances of the timer?memberMiiika_19 Oct '10 - 22:11 
QuestionNot accurate as C++ native mode [modified]memberBit_Flipper9 Jun '10 - 10:11 
GeneralThanks very muchmemberJulian11223 May '10 - 20:05 
Generalberlus@walla.co.ilmemberBerlus3 Feb '10 - 4:16 
GeneralThanksmember_brice2 Feb '10 - 1:50 
GeneralMake 'multiple' waitable timer using this Multi media timermemberMember 39606625 Jan '10 - 9:41 
GeneralRe: Make 'multiple' waitable timer using this Multi media timermemberBit_Flipper16 Jun '10 - 4:27 
GeneralRe: Make 'multiple' waitable timer using this Multi media timermemberMiiika_19 Oct '10 - 22:13 
Generalbroken examplesmemberSk9329 Sep '09 - 5:52 
GeneralRe: broken examplesmemberLeslie Sanford29 Sep '09 - 6:54 
GeneralProblem with accuracymemberOndrej8413 Feb '09 - 0:03 
QuestionProblem: Events are not being fired at the specified intervalmemberrkbowen21 Nov '08 - 1:44 
AnswerRe: Problem: Events are not being fired at the specified intervalmemberPittyAlex2 Jan '09 - 23:09 
QuestionThe Timer class is sealed, I cannot inherit it.memberuu88ii24 Jun '08 - 20:24 
Generalsorry,a mistakenly votememberuu88ii9 Jun '08 - 1:42 
GeneralRe: sorry,a mistakenly votememberIlíon18 Sep '08 - 14:25 
GeneralBug with TimerMode.OneShot and SyncronizingObjectmemberpatrickcbrown20 Sep '07 - 8:49 
GeneralRe: Bug with TimerMode.OneShot and SyncronizingObjectmemberLeslie Sanford20 Sep '07 - 8:57 
GeneralRe: Bug with TimerMode.OneShot and SyncronizingObjectmemberpatrickcbrown21 Sep '07 - 11:39 
GeneralRe: Bug with TimerMode.OneShot and SyncronizingObjectmemberLeslie Sanford21 Sep '07 - 11:48 
GeneralRe: Bug with TimerMode.OneShot and SyncronizingObjectmemberpatrickcbrown20 Sep '07 - 8:58 
GeneraltimeBeginPeriod/timeEndPeriodmemberujr14 Feb '07 - 6:29 
GeneralRe: timeBeginPeriod/timeEndPeriodmemberRuben Jönsson5 Oct '07 - 3:20 
GeneralCan't open demo form (Form1.cs) in designermemberDPRoberts10 Aug '06 - 9:38 
GeneralRe: Can't open demo form (Form1.cs) in designermemberDPRoberts10 Aug '06 - 9:42 
GeneralRe: Can't open demo form (Form1.cs) in designermemberLeslie Sanford10 Aug '06 - 11:02 
QuestiontimeSetEvent fundamentally flawed?memberhotbot8212 Jun '06 - 1:31 
AnswerRe: timeSetEvent fundamentally flawed?memberLeslie Sanford12 Jun '06 - 4:17 
GeneralRe: timeSetEvent fundamentally flawed?memberhotbot8212 Jun '06 - 10:55 
AnswerRe: timeSetEvent fundamentally flawed?memberkdanielsen30 May '07 - 3:58 
QuestionQuestion about threads and events?memberdmbrider11 May '06 - 10:35 
AnswerRe: Question about threads and events?memberLeslie Sanford11 May '06 - 11:40 
GeneralRe: Question about threads and events?memberdmbrider21 May '06 - 2:33 
GeneralUnstable behavior with NT4 and fast timer ratesmemberdan neely8 May '06 - 4:03 
GeneralRe: Unstable behavior with NT4 and fast timer ratesmemberLeslie Sanford8 May '06 - 5:01 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 1 Mar 2006
Article Copyright 2003 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid