功能:
1. 欲实现一个定时执行特定任务的程序,执行时间由一个CTimeCtrl设定,到了指定时间后,便启动任务。
代码:
1. 启动定时器代码:
if (!SetTimer(1,1000,NULL)) {
MessageBox(_T("Error: SetTimer failed!"));
return -1;
}
2. OnTimer代码:
CTime tTime;
((CDateTimeCtrl*)GetDlgItem(IDC_DATETIME))->GetTime(tTime);
if(tTime==CTime::GetCurrentTime()){
DoCommand();
}
CDialog::OnTimer(nIDEvent);
问题:
1. 如果将SetTimer(1,1000,NULL):将时间间隔设为1000ms,则会发生有时任务得不到执行的情况,而将间隔时间设为500ms,每次又会连续执行两次。请问这种情况应该如何解决???

解决方案 »

  1.   

    timeSetEvent
    The timeSetEvent function starts a specified timer event. The multimedia timer runs in its own thread. After the event is activated, it calls the specified callback function or sets or pulses the specified event object.MMRESULT timeSetEvent(
      UINT           uDelay,      
      UINT           uResolution, 
      LPTIMECALLBACK lpTimeProc,  
      DWORD_PTR      dwUser,      
      UINT           fuEvent      
    );
      

  2.   

    看MSDN嘛,那是多媒体定时器,比settime的精确度要高的多。