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

Wrapper Class for the Multimedia Timer Functions

, 23 Nov 2005
Rate this:
Please Sign up or sign in to vote.
A class that makes using the Multimedia Timer functions easy and painless.

Sample Image - mult_media_timer.jpg

Introduction

In many of the programs we write, we need to know when a certain amount of time has passed before doing something; we need timing information. In a Windows program, that often means using the WM_TIMER message. Sometimes, however, we need something a little more accurate, something a little more finely grained.

One option is to use the multimedia timer. This timer can give us greater accuracy and resolution than WM_TIMER messages. The CMMTimer class encapsulates the Windows multimedia functions to make using the multimedia timer easy and painless.

The CMMTimer and CMMTimerListener classes

The CMMTimer provides a set of methods for using the multimedia timer. Listed below are each method and a brief description of how to use it.

// Starts the timer. Delay is the interval in milliseconds between each 
// timing event. Resolution is the timing resolution of this timer. A value
// of zero gives the greatest timing resolution. Returns true if the 
// operation was successful.
bool Start(UINT Delay, UINT Resolution);

// Stops this timer. Timer can be restarted by calling Start.
void Stop();

// Resets this timer back to zero.
void Reset();

// Returns true if this timer is running.
bool IsRunning() const { return m_RunningFlag; }

// Functions for attaching, detaching, and notifying CMMTimerListeners
void AttachListener(CMMTimerListener &Listener);
void DetachListener(CMMTimerListener &Listener);
void NotifyListeners();

// Gets the total timing values since this timer was started.
DWORD GetTotalMilliseconds() const; 
DWORD GetTotalSeconds() const;
DWORD GetTotalMinutes() const;
DWORD GetTotalHours() const;

//
// The following accessor functions return relative timing values. For 
// example, the GetCurrentMilliseconds function returns how many 
// milliseconds have passed since the last second has occurred. Let's say  
// that 3.5 seconds have passed since the timer was started. 
// GetCurrentMilliseconds would return 500 because that is how many 
// milliseconds have passed since the last second began. 
//

DWORD GetCurrentMilliseconds() const;
DWORD GetCurrentSeconds() const;
DWORD GetCurrentMinutes() const;
DWORD GetCurrentHours() const;

// Gets the number of times a timing event has occurred.
DWORD GetCount() const;

// Gets the device capabilities of this timer. Results are stored in the
// TIMECAPS structure.
static void GetDevCaps(LPTIMECAPS TimeCap);

In order to receive timing events from a CMMTimer object, you will need to implement the CMMTimerListener class and attach your class to the CMMTimer object with the AttachListener method. For every timing event, the CMMTimer will notify all of its listeners that a timing event has occurred by calling the Update method in the CMMTimerListener objects.

Also, it's important to note that you must link to the winmm.lib before you can use the CMMTimer class.

The CMMTimer Demo App

I've included a toy application to demonstrate using the CMMTimer and CMMTimerListener classes. This program has a simple animation sequence. The speed of the animation is determined by a CMMTimer object. To start the animation, simply press the Start button, and to stop the animation press Stop. The speed can be adjusted by choosing from the settings listed in the Speed combo box.

Conclusion

Well, that's about it. I hope you find this class helpful when you need timing notification that's a little more accurate than what WM_TIMER messages can give you. Please let me know if any improvements can be made to the design or if there are additional features that would make the class more useful.

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

Share

About the Author

Leslie Sanford

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

 
Generaltnx Pinmembersacchigabri31-May-12 13:57 
QuestionThank you so much! Pinmemberecholone4-Apr-12 17:52 
Generalthanks Pinmembersmart_dummies10-Apr-10 22:17 
QuestionLittle question PinmemberGiang Son20-Apr-09 15:34 
- Thank you so much about the article. But it has a problem. When i press mouse keep the window (not drag, just press and keep), the timer is stopped. Would you help me how to fix this problem ?
Best regard
 
Giang Son

GeneralEnterCriticalSection warning PinmemberSindelaru14-Feb-08 1:30 
GeneralUpdated PinmemberLeslie Sanford23-Nov-05 5:24 
GeneralRe: Updated PinmemberPaul Selormey23-Nov-05 11:14 
GeneralRe: Updated PinmemberPaul Selormey23-Nov-05 11:40 
GeneralRe: Updated PinmemberLeslie Sanford23-Nov-05 11:50 
GeneralRe: Updated PinmemberPaul Selormey23-Nov-05 13:30 
QuestionLGPL? PinmemberChris Losinger23-Nov-05 3:14 
AnswerRe: LGPL? PinmemberLeslie Sanford23-Nov-05 4:30 
GeneralMultimedia And Waitable Timer PinsussAnonymous18-Aug-04 1:36 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 23 Nov 2005
Article Copyright 2002 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid