How to use VC high-precision Multimedia timer (II)

Source: Internet
Author: User
How to use VC high-precision Multimedia timer Author: cnfgg Date: 2009-08-19In VC programming, with SetTimer can define a timer, to the time, the response to OnTimer message, but this timer accuracy is too low. If you need a higher precision timer (accurate to 1ms), you can use the following high-precision multimedia timer for code optimization, can achieve millisecond accuracy, and easy to use. First, include the header file "Mmsystem.h" and the library file "Winmm.lib".

VC High-Precision multimedia timer using the following methods:

Copy content to Clipboard program code
#include "mmsystem.h"//head file

#pragma comment (lib, "winmm")//lib file

const 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::D estroytimer ()
{

if (BRun)
{
Timekillevent (Timerid);
Timeendperiod (Timeres);

BRun = FALSE;
}


}

/******************************************************************\
function
Name:timerhandler
Desc:timer procedure called when the 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;

}



Using the multimedia timer timesetevent () function, the function has a timing accuracy of MS class. The function can be used to implement periodic function calls. The prototype of the function is as follows:
Mmresult timesetevent (UINT udelay,
UINT Uresolution,
Lptimecallback Lptimeproc,
WORD Dwuser,
UINT fuevent)

This function sets a timed callback event, which can be a one-time event or a recurring event. Once the event is activated, the specified callback function is invoked, the identifier code of the event is returned after success, otherwise NULL is returned. The parameters of the function are described as follows:

Udelay: Specifies the period of the event in milliseconds.
Uresolution: Specify the precision of the delay in milliseconds, the smaller the value the higher the timer event resolution. The default value is 1ms.
Lptimeproc: Point to a callback function.
Dwuser: Store user-provided callback data.
Fuevent: Specifies the Timer event type:
Only one event is generated after Time_oneshot:udelay milliseconds
       Time_periodic: Events occur periodically every udelay millisecond.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.