Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delay between commands

Status
Not open for further replies.

Striker99

Programmer
Oct 31, 2001
34
UA
How to wait for x seconds between commands in VB?
 
Just be advised that if you use the Sleep API call, you won't be able to process any events while it's sleeping. So you won't be able to repaint the screen, use any controls, or anything like that. If that isn't a problem, then Sleep is fine. If it is a problem, use a timer.
 
This may help...

Function Delay%(sglDelayIntvl As Single)
'===========================================================
'
' Desc: This function delays for N 1/100's seconds.
' This is necessary because some screen flips free the
' Presentation Space a second before the next
' screen arrives.
'
' The Windows Sleep API is called to execute the
' delay. This API is recommended for 32 bit
' Visual Basic applications for "Waiting" in code.
'
' DoEvent statements are still issued so that: the
' "Busy" Form is updated by allowing the Timer
' control some time; and allow Windows to be more
' responsive to mouse events and painting, etc.
'
' Parms: sglDelayIntvl - number of seconds to wait before returning.
'
' Returns: 0 - Successful
' ELP_INVALID_WAIT_INTVL - Delay is not between 0 - 60 seconds
'
'===========================================================

On Error GoTo Delay_Err

Const OAS_MAX_WAIT_SECS = 60# 'Maximum wait allowed is 60 seconds
Const DELAY_INTERVAL = 250 'Default delay interval (.25 seconds/250 Milliseconds)

Dim sglStartTime As Single
Dim sglEndTime As Single
Dim sglElapsedTime As Single
Dim lngdelay As Integer

If Not ((sglDelayIntvl > 0) And (sglDelayIntvl < OAS_MAX_WAIT_SECS)) Then
Delay% = ELP_INVALID_WAIT_INTVL
Exit Function
End If

' Convert delay time to the format (milliseconds) needed by the Sleep API
lngdelay = sglDelayIntvl * 1000

sglStartTime = Timer

' Wait for specified amount of time, but issue DoEvents at regular
' intervals, in order for forms to be updated and mouse events to
' be processed.
Do While (lngdelay > 0)

If (lngdelay > DELAY_INTERVAL) Then
Call Sleep(DELAY_INTERVAL)
lngdelay = lngdelay - DELAY_INTERVAL
Else
Call Sleep(lngdelay)
lngdelay = 0
End If

' Allow forms to be updated and mouse events to be processed
DoEvents

Loop

sglEndTime = Timer

sglElapsedTime = sglEndTime - sglStartTime
gsglElapsedTime = sglEndTime - gsglStartTime

Debug.Print &quot;Delay, executed for time of &quot;; sglElapsedTime; Space(4); &quot;Split Time: &quot;; gsglElapsedTime

Delay% = 0

Exit Function

Delay_Err:
Call KillTheScript(Err, &quot;Delay&quot;, &quot;&quot;, True)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top