Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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___


class CMMTimers
 CMMTimers(UINT resolution);
 virtual ~CMMTimers();

 UINT	getTimerRes() { return timerRes; };

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

 virtual void timerProc() {};

 UINT	timerRes;
 UINT	timerId;


The source file, mmTimers.cpp

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

CMMTimers::CMMTimers(UINT resolution) : timerRes(0), timerId(0)

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

 if (0 != timerRes)
  timerRes = 0;

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


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

 result = timeSetEvent(period, timerRes, internalTimerProc,
 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


  • Question about callback

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

    Originally posted by: Scott

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

  • a sample program for mmTimers

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

    Originally posted by: crico

    I hope that it will help!
    I work with VC6 and Win2K.


    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 09:47am

      My email : wuyanping3092@163.com

    • I need your sample code!

      Posted by firebirdgo on 06/24/2005 07: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

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

      Posted by s-gm on 06/03/2004 04:55pm

      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

  • Problem with huge interval

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

    Originally posted by: Caroline


    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


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

    Originally posted by: Melissa

    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!)

  • I understand but I can't use it

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

    Originally posted by: Georgi

    Callback problem!!!

  • You can have too many timers

    Posted by Legacy on 07/18/2002 07: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.

  • Some questions

    Posted by Legacy on 07/08/2002 07: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) ?


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

    Posted by Legacy on 12/10/2001 08: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.

  • Help !! Another question

    Posted by Legacy on 07/12/2001 07: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???

  • Thanks a lot *^-^*

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

    Originally posted by: Keeook, Park

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

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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