关于延时函数的问题(5分)

  • 主题发起人 主题发起人 dorble
  • 开始时间 开始时间
D

dorble

Unregistered / Unconfirmed
GUEST, unregistred user!
在一个程序需要大量使用延时,开始用sleep然后发现程序运行时挂起状态,然后用这个procedure Delay(msecs:integer);<br>var <br>&nbsp; Tick: DWord;<br>&nbsp; Event: THandle;<br>begin<br>&nbsp; Event := CreateEvent(nil, False, False, nil);<br>&nbsp; try<br>&nbsp; &nbsp; Tick := GetTickCount + DWord(msecs);<br>&nbsp; &nbsp; while (msecs &gt; 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) &lt;&gt; WAIT_TIMEOUT) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; msecs := Tick - GetTickcount;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; CloseHandle(Event);<br>&nbsp; end;<br>end;<br><br>也有问题感觉还不如sleep<br>有人给我下面这个函数procedure Delay(msecs: integer);<br>var<br>&nbsp; &nbsp; FirstTickCount: Longint;<br>begin<br>&nbsp; &nbsp; FirstTickCount := GetTickCount;<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.ProcessMessages();<br>&nbsp; &nbsp; &nbsp; &nbsp; sleep(1);<br>&nbsp; &nbsp; until ((GetTickCount - FirstTickCount) &gt;= Longint(msecs));<br>end;<br>为什么里面有一句sleep(1)那不是也用了sleep这个函数吗?<br>谁告诉到底那个函数好用?
 
直接这样用就可以吧,<br>procedure Delay(msecs: integer);<br>var<br>&nbsp; &nbsp; FirstTickCount: Longint;<br>begin<br>&nbsp; &nbsp; FirstTickCount := GetTickCount;<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.ProcessMessages();<br>&nbsp; &nbsp;// &nbsp; &nbsp; sleep(1);<br>&nbsp; &nbsp; until ((GetTickCount - FirstTickCount) &gt;= Longint(msecs));<br>end;
 
把下面的sleep(1)注释掉了分给你,给我解释一下好吗?
 
接受答案了.
 
后退
顶部