About   -   Contact   -   Purchase   -   Search   -   What's New

Creating Time Delays
Creating time delays is often required in applications for various reasons, e.g., flashing a message to the user by writing, say, 'All Changes Saved...' to the StatusBar and deleting it after a couple of seconds. There are several way to do this, but the simplest way is a For..Next loop of a predetermined size. The only drawback with that method is the time delay is then dependent on processor speed. However a simple idea, again employing a loop is to use the Timer property to determine the point of exiting the loop. In the procedure below the required time delay is passed into the procedure and the loop is executed until the Timer recognizes the time delay has elapsed. In the event a large time delay is required, a condition is added to yield so the operating system can process other events in addition to the timer loop.
Public Sub Delay(sngSeconds As Single)

    Dim timSnapShot As Single
    Dim timYieldCheck As Single
    timSnapShot = Timer
    timYieldCheck = Timer
    Do Until Timer > timSnapShot + (sngSeconds)
        'yield to Windows every 10 seconds
        If Timer > timYieldCheck + 10 Then
            timYieldCheck = Timer
        End If
End Sub
If only short time delays are required then consider using an API to control the delay such as the Sleep API shown below. The only limitation being the absence of any OS yield event.
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub WindowsSleep(sngSeconds As Single)

   Dim lngMilliseconds As Long

   lngMilliseconds = 1000& * CLng(sngSeconds)
   Call Sleep(lngMilliseconds)

End Sub

Top of Page

Legal Notice

Ken Howe 2011