Advertisement
C_Volume2 VB function enhancement #67638

New Sleep Method

When you are writing a sub or a function you maybe want to implement a delay. The most common way is to use a DoEvents loop. But then the CPU will be 100% busy. Another way is to use the Sleep API. But then your app won't respond to any events while waiting. Way 3 sets a timer and exit the function. The timer will start the function again. But then you have to implement a state machine into every delayed function. The following code won't stress the CPU, keeps your app responsive and it is easily to implement. The only requirement is that you insert it into the code of a window with a hWnd.

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
original-source
Private Sub Sleep(ByVal MilliSeconds As Long)
 Dim Message As MSG, TimerID As Long
 TimerID = Int(Rnd * 2 ^ 32 - 2 ^ 31)
 TimerID = SetTimer(hWnd, TimerID, MilliSeconds, 0)
 If TimerID = 0 Then Exit Sub
 Do
  DoEvents
  WaitMessage
  If PeekMessage(Message, hWnd, WM_TIMER, WM_TIMER, PM_NOREMOVE) Then
   If Message.wParam = TimerID Then Exit Do
  End If
 Loop
 KillTimer hWnd, TimerID
End Sub
Original Comments (3)
Recovered from Wayback Machine