您好,我想向您请教一下关于程序延时的问题,请您指点。
前段时间请教过您关于定时器的问题,而且已经高明白了,但是感觉优先级不高,延时比较大,所以采用了如下的延时函数:
void CAutoBuxView::mySleep(int seconds)
{
CString strTmp; COleDateTime     start_time     =     COleDateTime::GetCurrentTime();       
COleDateTimeSpan     end_time     =     COleDateTime::GetCurrentTime()-start_time;       
while(end_time.GetTotalSeconds()     <  seconds)       
{       
MSG     msg;       
GetMessage(&msg,NULL,0,0);       
TranslateMessage(&msg);     
DispatchMessage(&msg);     
end_time=COleDateTime::GetCurrentTime()-start_time;  
strTmp.Format("%f",seconds-end_time.GetTotalSeconds());
strTmp="Waiting for "+strTmp+" seconds";
((CMainFrame*)AfxGetMainWnd())->m_wndDlgBar.GetDlgItem(IDC_STATIC_Status)->SetWindowText(strTmp);
}     
return ;
}
现在也能够实现延时,但是我发现存在一定的问题:
如果焦点在该程序中,则计时比较及时准确,否则延时很大。做过一个很明显的实验例子就是:当鼠标在别的程序上,显示的时间几乎不变化,而如果将鼠标移动到该界面就会及时更新。
我现在想实现即使焦点不在该程序上也能及时更新,您能指点一下吗?谢谢了

解决方案 »

  1.   

    void   CAutoBuxView::mySleep(int   seconds) 

    CString   strTmp; COleDateTime           start_time           =           COleDateTime::GetCurrentTime();               
    COleDateTimeSpan           end_time           =           COleDateTime::GetCurrentTime()-start_time;               
    while(end_time.GetTotalSeconds()           <     seconds)               
    {               
    MSG           msg;     
    if(PeekMessage(&msg, NULL, 0, 0,   PM_NOREMOVE))
    {        
    GetMessage(&msg,NULL,0,0);               
    TranslateMessage(&msg);           
    DispatchMessage(&msg);  
    }         
    end_time=COleDateTime::GetCurrentTime()-start_time;     
    strTmp.Format("%f",seconds-end_time.GetTotalSeconds()); 
    strTmp="Waiting   for   "+strTmp+"   seconds"; 
    ((CMainFrame*)AfxGetMainWnd())-> m_wndDlgBar.GetDlgItem(IDC_STATIC_Status)-> SetWindowText(strTmp); 
    }           
    return   ; 
      

  2.   

    要使用高精度定时器,一定要使用线程。
    要么在线程里创建一个循环:while(TRUE){Sleep(1000);PostMessage(m_hwnd,...);}
    要么使用CreateWaitableTimer创建高精度定时器
      

  3.   

    给LZ推荐一个VC的定时器使用,有源码和大量注释,LZ一定要试下:
    http://download.csdn.net/detail/txzsp/2285350