A Class To Encapsulate MultiMedia Timers

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

This simple class encapsulates the multimedia timers. To use the class include mmTimers.cpp and mmTimers.h in your project. Link with winmm.lib. To use a timer derive a class from CMMTimers and override member timerProc. timerProc will be called when the timer goes off. Instantiate a variable of the new class. The parameter to the constructor is the timer resolution in ms. To start a timer call startTimer. The first parameter specifies the period of the timer in ms. The second parameter specifies whether the timer is a one shot or periodic timer. To stop a periodic timer call stopTimer.

This code was developed with Visual C++ 5.0 and has been tested on NT 4.0.

The source follows:

The header file, mmTimers.h


#ifndef	___multimedia_timers___
#define	___multimedia_timers___


#include	



class CMMTimers
{
public:
 CMMTimers(UINT resolution);
 virtual ~CMMTimers();

 UINT	getTimerRes() { return timerRes; };

 bool	startTimer(UINT period,bool oneShot);
 bool	stopTimer();

 virtual void timerProc() {};

protected:
 UINT	timerRes;
 UINT	timerId;
};


#endif


The source file, mmTimers.cpp


#include	"StdAfx.h"
#include	"mmTimers.h"


CMMTimers::CMMTimers(UINT resolution) : timerRes(0), timerId(0)
{
 TIMECAPS	tc;

 if (TIMERR_NOERROR == timeGetDevCaps(&tc,sizeof(TIMECAPS)))
 {
  timerRes = min(max(tc.wPeriodMin,resolution),tc.wPeriodMax);
  timeBeginPeriod(timerRes);
 }
}


CMMTimers::~CMMTimers()
{
 stopTimer();
 if (0 != timerRes)
 {
  timeEndPeriod(timerRes);
  timerRes = 0;
 }
}


extern "C"
void CALLBACK internalTimerProc(UINT id, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
 CMMTimers *	timer = (CMMTimers *)dwUser;

 timer->timerProc();
}


bool CMMTimers::startTimer(UINT period,bool oneShot)
{
 bool		res = false;
 MMRESULT	result;

 result = timeSetEvent(period, timerRes, internalTimerProc,
 (DWORD)this,oneShot ? TIME_ONESHOT : TIME_PERIODIC);
 if (NULL != result)
 {
  timerId = (UINT)result;
  res = true;
 }

 return res;
}


bool CMMTimers::stopTimer()
{
 MMRESULT	result;

 result = timeKillEvent(timerId);
 if (TIMERR_NOERROR == result)
  timerId = 0;

 return TIMERR_NOERROR == result;
}


Download source - 1 KB

Date Last Updated: May 17, 1999



Comments

  • Question about callback

    Posted by Legacy on 11/24/2003 12:00am

    Originally posted by: Scott

    Could you please describe the parameters and usage of the callback function.

    Reply
  • a sample program for mmTimers

    Posted by Legacy on 03/21/2003 12:00am

    Originally posted by: crico

    http://www.geocities.com/giccrico/download/MMTimersTest.zip
    I hope that it will help!
    I work with VC6 and Win2K.

    Regards,
    crico

    P.S. I want to thanks to Stefan (a friend of mine) for the callback idea

    • I need a sample

      Posted by wuyanping on 07/04/2006 02:47am

      My email : wuyanping3092@163.com

      Reply
    • I need your sample code!

      Posted by firebirdgo on 06/24/2005 12:01am

      Dear Sir:
        I couldn't download your example code from the link provided on the page above. So I wrote this letter to inquire if I can get a copy of your sample code from the email. Your early and kind reply will be appreciated greatly! Looking forward to your honored sample program for mmTimers! My email : firebirdgo@163.com

      Reply
    • Need your sample program for mmTimers! from a c++ beginner

      Posted by s-gm on 06/03/2004 09:55am

      Dear Sir, I came to know you when I serached sample program for multimedia timers on the web page http://www.codeguru.com/Cpp/G-M/multimedia/timers/comments.php/c1581/?thread=19992. but i couldn't download your honored code from the link provided on the page above. So I wrote this letter to inquire if I can get a copy of your sample code from the email. Your early and kind reply will be appreciated greatly! Looking forward to your honored samplr program for mmtimers! a c++ beginner s-gm@sohu.com

      Reply
    Reply
  • Problem with huge interval

    Posted by Legacy on 02/11/2003 12:00am

    Originally posted by: Caroline

    Hi,

    I have implemented this timer, All looks work good, I never get error, but when the interval is more 30 minutes, never the timer wake up.
    Have you got an idea ?
    Thanks a lot

    Reply
  • PLEASE HELP ME!

    Posted by Legacy on 02/04/2003 12:00am

    Originally posted by: Melissa

    HI!
    I am a student (trying to graduate this year!). I have a read and write function to/from the parallel port registers that I need to happen every 20 ms. I am trying to use the Multimedia timer with your code. I just can't figure out how to call my function for the callback function. Could you show me how?
    Thanks (I hope!)
    melissa

    Reply
  • I understand but I can't use it

    Posted by Legacy on 07/31/2002 12:00am

    Originally posted by: Georgi

    Callback problem!!!

    Reply
  • You can have too many timers

    Posted by Legacy on 07/18/2002 12:00am

    Originally posted by: Jim Moore

    I just uncovered an interesting limitation of multimedia timers (timeSetEvent). I have an application that has multiple threads. Each thread makes use of a timer. The timer is used to sleep for a period of time. When the thread count is 17, timeSetEvent begins returning NULL (failure). So, if you have an inkling to use more than 16 multimedia timers, you better figure out another way to do this. I am running Windows 2000.

    Reply
  • Some questions

    Posted by Legacy on 07/08/2002 12:00am

    Originally posted by: yuval

    What's the use of the resolution attribute in the constrcutor ?
    What is the maximum intervals I can use (or how I can get this value) ?

    Thanks

    Reply
  • Please do not use multimedia timer in Windows 9x/NT

    Posted by Legacy on 12/10/2001 12:00am

    Originally posted by: Edwin Zhang

    Multimedia timer was designed for 16-bit Windows, such as Windows 3.1. It will cause serious problem in Windows 95 and above. You can see MSDN to ensure this.

    Reply
  • Help !! Another question

    Posted by Legacy on 07/12/2001 12:00am

    Originally posted by: ohenry

    I try to use mmtimer in my system .The problem is that the value returned by the timeGetTime function is a DWORD value. The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days. This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution.
    How can I run my system longer than 50 days if I use timeGetTime???

    Reply
  • Thanks a lot *^-^*

    Posted by Legacy on 05/20/2001 12:00am

    Originally posted by: Keeook, Park

    I saw some "Light rays" in my project by your method.
    Thanks. ^^

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date