[Reprint] How to Use VC high-precision Multimedia Timer

Source: Internet
Author: User

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.


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.