Click here to Skip to main content
15,889,378 members
Articles / Desktop Programming / MFC
Article

A simple use of the Multimedia Timer

Rate me:
Please Sign up or sign in to vote.
4.61/5 (15 votes)
11 Mar 20031 min read 82K   3.7K   30   8
Demonstrates the simple use of a multimedia timer in user classes

Introduction

This article is about the use of a timer in every class you could write. When I want to use a timer in my applications, I always have the same problem.  The problem is that I'd like to be able to include a timer in a class which needs it without taking into account the fact that this class could be a CWnd, or a simple CDummyClass. The idea, which is quite simple is to use the template to perform such a work and also use MultiMediaTimer

The main trouble with the Multimedia timer is the callback routine that you are going to use to do the job, I've just tried to make it clear and simple. If this article sounds bad to your ears, I'll try to do better next time. Forgive me if this is so, as it's my first article ;-)

Using the code

It's very easy to use this class.  You only have to include the <clock.h>file and then instantiate a variable like this

CClock<THEMOTHERCLASS > myClock; 

For instance, if you intend to insert a clock in your CMyClass, you just add this CClock<CMYCLASS> myClock; The last thing you have to do to end the process is to create a function called TimerHappened(); This function will be called every time the timer is hit. This timer can be set with different value coming straight from the multimedia timers.

#include "clock.h"

//somewhere in your code

CClock<CMYCLASS> myClock;
myClock.SetCallBack(this);

myClock.SetDelay(100,TIME_ONE_SHOT); // in ms for a one shot timer

// start the clock

myClock.start();

void CMyClass::TimerHappened(int id)
{
   TRACE("I've been hit once in my life time\n");
}

of course , there are many method that you could call which are :

- pause()
- resume()

Points of Interest

Writing this code made me remember the use of the template... :-)

History

This is the V1.0 of this timer. I'll make change if I need it or if some asks for it...

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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
espinc7-Sep-10 4:13
espinc7-Sep-10 4:13 
QuestionPrecision of this algorithm? Pin
LMCer30-May-07 7:33
LMCer30-May-07 7:33 
QuestionHow to reset timer? Pin
firstluv8125-Sep-06 9:38
firstluv8125-Sep-06 9:38 
Questionquestion? Pin
Electro 17-Oct-05 11:37
Electro 17-Oct-05 11:37 
Generalexternal errors Pin
Member 88407019-Feb-04 13:55
Member 88407019-Feb-04 13:55 
GeneralRe: external errors Pin
Member 88407019-Feb-04 15:01
Member 88407019-Feb-04 15:01 
GeneralWord of caution: MultimediaTimer = Multithreading Pin
Damir Valiulin18-Mar-03 5:26
Damir Valiulin18-Mar-03 5:26 
Quite an elegant solution with template class. Good job!

I should warn you however, that multimedia timer is simply another high priority thread that executes a callback function you specify. Therefore all the precautions about object synchronization in multithreaded application also apply when you start using multimedia timers.

For example, in your Clock class, when timer calls the timerRoutine callback and you access member variables such as m_timerID and m_mode, they could've been reset by another event, such as user pressing Start/Stop button. In your example the chances are very low, of course, but it can happen.

Also 3 more things about the code:

1.What is the purpose of SetEvent? It doesn't look like it's being used.

2. There's typo (TIME_ONE_SHOT instead of TIME_ONESHOT). The only reason compiler doesn't complain is because the function is not used for this template class. And why is it ONESHOT? when it should be a m_mode?

3. Same as in q2. Why do you start PERIODIC timer and not m_mode timer?

Damir
GeneralRe: Word of caution: MultimediaTimer = Multithreading Pin
The Monz24-Mar-03 1:56
The Monz24-Mar-03 1:56 

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.