天天看點

VC函數中的延時操作

說到程式中的延時,你會想到怎麼做,新開一個線程?如果我的程式隻用單線程,

卻又想讓函數等上10秒才傳回值,而且還不能像使用Sleep函數那樣不能處理其它消息呢?

我在這裡把論壇裡能見到的幾種延時方式總結一下。

-------------------------------------------------------------------------------

見過不隻一個人問起過。

其實估計陌生人是直接手寫的這段代碼,不是從程式段中copy出來的,有一些手誤,大家自己調整一下就行了

#include

COleDateTime  start_time = COleDateTime::GetCurrentTime();

COleDateTimeSpan  end_time = COleDateTime::GetCurrentTime() - start_time;

while(end_time.GetTotalSeconds()  <=  2)

{    

MSG  msg;    

GetMessage(&msg,NULL,0,0);  

 TranslateMessage(&msg);  

DispatchMessage(&msg);  

end_time = COleDateTime::GetCurrentTime() - start_time;

}

注意到我把原文中的 PreTranslateMessage(&msg);

替換為了: TranslateMessage(&msg);  DispatchMessage(&msg);

原因是,可以不僅僅在MFC中使用,而且 PreTranslateMessage有局限性,而且可能會造成線程消息阻塞。      

還有一點說明,因為COleDateTimeSpan類的成員函數還有:GetTotalMinutes、GetTotalHours、GetTotalDays,

能夠實作更大時間段的延時。

-----------------------------------------------------------------------

往更小的時間跨度上說,執行毫秒級的延時用GetTickCount就行:

DWORD dwStart = GetTickCount();DWORD dwEnd = dwStart;

do{  

 MSG  msg;    

GetMessage(&msg,NULL,0,0);    

TranslateMessage(&msg);  

DispatchMessage(&msg);  

dwEnd = GetTickCount();

} while((dwEnd - dwStart)  <=  2000);

--------------------------------------------------------------------

然後是微秒級延時:

LARGE_INTEGER  litmp ;

LONGLONG  QPart1,QPart2 ;

double d=0;

QueryPerformanceCounter(&litmp) ; // 獲得初始值QPart1 = litmp.QuadPart ;while (d<40)//你想要的時間{    QueryPerformanceCounter(&litmp) ;    

QPart2 = litmp.QuadPart ;  

d=(double)(QPart2 - QPart1);

}

未做修改,如果需要微秒級的延時中也處理消息,請參照前例修改。

最後,如果還不能滿足,那就去做時鐘周期的延時吧:

#define NOP_COUNT 3//需要自己根據NOP及LOOP的指令周期計算.__asm

{  MOV ECX, NOP_COUNTDELAY: NOP  LOOP DELAY }

繼續閱讀