c++定時器

定時器有兩種方式:windows

一:MFC基於CWnd的Timer,指定定時器ID,響應WM_TIMER消息函數OnTimer就OK了。函數

二:基於windows API的Timer,spa

設定定時器nIDEvent:io

       TIMECAPS tc;
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) == TIMERR_NOERROR)
{
//分辨率的值不能超出系統的取值範圍
nAccuracy = min(max(tc.wPeriodMin, TIMER_ACCURACY), tc.wPeriodMax);
//調用timeBeginPeriod函數設置定時器的分辨率,相似於for循環的步長
int nResult = timeBeginPeriod(nAccuracy);
//設置定時器
nResult = timeSetEvent(nIDEvent,
nAccuracy,
(LPTIMECALLBACK)LogTimeProc,
NULL,
TIME_PERIODIC);
}for循環

 關閉定時器nIDEvent:循環

       timeKillEvent(nIDEvent);
timeEndPeriod(nAccuracy);定時器

定時器響應函數:im

     void PASCAL LogTimeProc(UINT wTimerID, 
UINT  msg,
DWORD dwUser,
DWORD dw1,
DWORD dw2)
{time

;
}消息