A Class To Encapsulate MultiMedia Timers | CodeGuru

A Class To Encapsulate MultiMedia Timers

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 17, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.