65.9K
CodeProject is changing. Read more.
Home

A simple use of the Multimedia Timer

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.61/5 (14 votes)

Mar 12, 2003

1 min read

viewsIcon

82868

downloadIcon

3663

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 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...