From: http://www.cnfgg.com/article/Vc/vc_mmTimer.htm
Author: cnfgg Date:
In VC programming, settimer can be used to define a timer. When the time is reached, the timer will respond to the ontimer message, but the timer accuracy is too low. If you need a timer with a higher accuracy (accurate to 1 ms), you can use the following high-precision multimedia timer to optimize the code, which can achieve millisecond-level precision and ease of use. First, you must include the header file "mmsystem. H" and the library file "winmm. lib ".
The VC high-precision multimedia timer is used as follows:
#include "mmsystem.h" //head file#pragma comment(lib,"winmm") //lib fileconst int timePeriod = 2;const int timeRes = 1 ;/*******************MMTimer fuction********************************\ CreateTimer : create a Multimedia timer DestroyTimer: destroy a Multimedia timer TimerHandler: the actual timer handler procedure\******************************************************************//******************************************************************\ function name : CreateTimer desc : create a realtime timer argument void ret code [HANDLE] ,the handle of the timer\******************************************************************/UINT CMyTimer::CreateTimer(){ //create the timer // Create a periodic timer timeBeginPeriod(timeRes); timerID = timeSetEvent( timePeriod, timeRes, TimerHandler, (DWORD)this, TIME_PERIODIC); return timerID;}/******************************************************************\ function name : DestroyTimer desc : destroy the timer created by calling CreateTimer argument void ret code void\******************************************************************/void CMyTimer::DestroyTimer(){ if ( bRun ) { timeKillEvent(timerID); timeEndPeriod(timeRes); bRun = FALSE; }}/******************************************************************\ function name : TimerHandler desc : timer procedure called when the the timer signaled argument dwUser,[in],user para data ret code void\******************************************************************/void CALLBACK CMyTimer::TimerHandler(UINT id, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2){ CMyTimer* pThis = (CMyTimer*)dwUser;}
The multimedia timer timesetevent () function is used. The timer precision of this function is ms. This function can be used to implement periodic function calls. The function prototype is as follows:
Mmresult timesetevent (uint udelay,
Uint uresolution,
Lptimecallback lptimeproc,
Word dwuser,
Uint fuevent)
This function sets a scheduled callback event, which can be a one-time event or periodic event. Once an event is activated, the specified callback function is called. If successful, the code of the event identifier is returned. Otherwise, null is returned. Function parameters are described as follows:
Udelay: Specifies the event cycle in milliseconds.
Uresolution: Specify the latency precision in milliseconds. The smaller the value, the higher the timer event resolution. The default value is 1 ms.
Lptimeproc: point to a callback function.
Dwuser: stores user-provided callback data.
Fuevent: Specify the timer event type:
Time_oneshot: Only one event is generated after udelay in milliseconds
Time_periodic: events are generated cyclically every udelay millisecond.