sleep()会引起主线程的阻塞,表现在界面上,就是不会动,也就是会影响业务操作。<br>{MyDelay1--use GetTickCount}<br>procedure mydelay1(msecs:dword);stdcall;//不会影响业务操作,但占用CPU大<br>var<br> begintime:dword;<br>begin<br> begintime:=gettickcount;<br> repeat<br> Application.ProcessMessages;<br> until gettickcount-begintime>=msecs;<br><br>end;<br>{MyDelay2--use QueryPerformanceCounter}<br>procedure mydelay2(msecs:dword);stdcall;//不会影响业务操作,但占用CPU较多<br>var<br> rstart,rend,rfreq:TLargeInteger;<br>begin<br> {取得频率}<br> QueryPerformanceFrequency(rfreq);<br> rend:=round(msecs*rfreq/1000);<br> QueryPerformanceCounter(rstart);<br> rend:=rend+rstart;<br> repeat<br> QueryPerformanceCounter(rfreq);<br> Application.ProcessMessages;<br> until rfreq>=rend;<br>end;<br>{mydelay3--use queryperformancecounter with calibration}<br>procedure mydelay3(msecs:dword);stdcall;//不会影响业务操作,但占用CPU小些,延时时间也比较准确,尤其是在毫秒级。<br>var<br> rstart,rend,rfreq:TLargeInteger;<br>begin<br> QueryPerformanceFrequency(rfreq);<br> rend:=Round(msecs*rfreq/1000);<br> QueryPerformanceCounter(rstart);<br> rend:=rend+rstart-PerformanceCounterOverhead*2;<br> repeat<br> QueryPerformanceCounter(rfreq);<br> Application.ProcessMessages;<br> until rfreq>=rend;<br>end;<br>{call the performance counter to determine the time overhead}<br>function CalibratePerformanceCounterOverhead:TLargeInteger;<br>var<br> I:integer;<br> rstart,rend:TLargeInteger;<br>begin<br> QueryPerformanceCounter(rstart);<br> for i:=1 to 1000 do<br> QueryPerformanceCounter(rend);<br> result:=round((rend-rstart)/1000);<br>end;