VBNotebook Home | Functions | Pause
This routine pauses execution for specified number of seconds. If you choose to AllowEvents, this waiting program will use more system resources, but the program won't appear to be locked up to users. If you decide not to allow events, the program will appear to be locked up to users, possibly causing them to kill the application from the task manager or even reboot their system.
First, In General|Declarations....
__________________________________________________________________________________________
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
__________________________________________________________________________________________
And the routine itself....
__________________________________________________________________________________________
Sub Pause(ByVal fSeconds As Single, Optional ByVal AllowEvents As Boolean = True)
Dim fTimer As Single
If AllowEvents Then
fTimer = timer
Do While timer - fTimer < fSeconds
'
' Optional Line To reduce CPU usage during loop
'
Sleep 100
DoEvents
'
' if we cross midnight, back up one day
'
If timer < fTimer Then
fTimer = fTimer - 86400 'one day in seconds 24*60*60
End If
Loop
Else
Sleep fSeconds * 1000
End If
End Sub
__________________________________________________________________________________________
Note the optional Sleep statement in the AllowEvents section of the code.
This allows a combination of the two methods by causing a short sleep that
probably won't be noticed by the user during the wait loop.
Another option, not covered here, is using the SetWaitableTimer API calls. This method is not available in Win95 or NT 3.51. You can find a sample routine in this MSDN library article:
HOWTO: Use SetWaitableTimer With Visual Basic
ID: Q231298
__________________________________________________________________________________________
Copyright 2000-2005, J. Frank Carr