No CPU Load - SafeSleep
The *BEST* way to safely Sleep/Pause without taxing the CPU.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
<b> The two functions below both sleep for the specified number of seconds. However, the popular code seen in the BusySleep procedure actually causes the CPU load to stay near 100% until complete. The SafeSleep routine pauses without taxing the CPU and stays at nearly 0% CPU. Both functions take a single value so you can sleep for fractions of a second.</b> <br><br> <tt> Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long<br><br> Public Sub SafeSleep(ByVal inWaitSeconds As Single)<br> <nbsp><nbsp> Const WAIT_OBJECT_0 As Long = 0<br> <nbsp><nbsp> Const WAIT_TIMEOUT As Long = &H102<br><br> <nbsp><nbsp> Dim lastTick As Single<br> <nbsp><nbsp> Dim timeout As Long<br> <nbsp><nbsp> timeout = inWaitSeconds * 1000<br> <nbsp><nbsp> lastTick = Timer<br><br> <nbsp><nbsp> Do<br> <nbsp><nbsp><nbsp><nbsp> Select Case MsgWaitForMultipleObjects(0, 0, False, timeout, 255)<br> <nbsp><nbsp><nbsp><nbsp> Case WAIT_OBJECT_0<br> <nbsp><nbsp><nbsp><nbsp> DoEvents<br> <nbsp><nbsp><nbsp><nbsp> timeout = ((inWaitSeconds) - (Timer - lastTick)) * 1000<br> <nbsp><nbsp><nbsp><nbsp> If timeout < 0 Then timeout = 0<br><br> <nbsp><nbsp><nbsp><nbsp> Case Else<br> <nbsp><nbsp><nbsp><nbsp> Exit Do<br><br> <nbsp><nbsp><nbsp><nbsp> End Select<br><br> <nbsp><nbsp> Loop While True<br><br> End Sub<br><br> Public Sub BusySleep(ByVal inWaitSeconds As Single)<br> <nbsp><nbsp> Dim lastTick As Single<br><br> <nbsp><nbsp> lastTick = Timer<br><br> <nbsp><nbsp> Do<br> <nbsp><nbsp><nbsp><nbsp> DoEvents<br><br> <nbsp><nbsp> Loop While (Timer - lastTick) < inWaitSeconds<br><br> End Sub<br> </tt>
Original Comments (3)
Recovered from Wayback Machine