Pause for 5 seconds

In a vb5 script, I have a loop.  I want to have it pause for 5 seconds every time it iterates.
while (i <= 100) {
# do some stuff
# pause 5 seconds
)
What code would I put in to cause the pause?
donb1Asked:
Who is Participating?
 
MaximkaConnect With a Mentor Commented:
dim vTimer as  Variant

vTimer=Timer
do While True
    If Timer - vTimer > 5 then
       exit do
    end if
loop

Hope that will helps You .
0
 
deightonCommented:
in a  .bas

Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

then
sleep(5000)   '5000 millisecs = 5secs


0
 
VBGuruCommented:
do you have timer function availble for you?

Then you can do like this?

dim l as long
l = timer + 60*5
while l > timer
wend
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lutharCommented:
Dim timedone As Single

Do While i <= 100
'instruction goes here

'Pause sequence here
      Let timedone = Timer
      Do While Timer < timedone + 5
      Loop
'Pause sequence finish
      i = i + 1
Loop

This should work ....
0
 
VbmasterCommented:
The best solution is to combine the do..loop with the Sleep API and add a DoEvents inside the do..loop block, something like this..

vTimer=Timer
Do Until (Time - vTimer > 5)
  DoEvents
  Sleep(100)
Loop

Because if you do a do..loop or a Sleep API only the application will freeze those five seconds and that will look like ****. The solution above will not freeze the redraw of the program AND will take less CPU time than the other solutions.

But this solution might not be applicable to your application because the DoEvents statement will make the program able to click on buttons on your program when the app is running the do..loop.
0
 
donb1Author Commented:
Maximka's comment works, the others don't work for me. At least it is the simplest solution for me.  Suggest Maximka make yours an answer.  Thanks also to the others for their interest.
0
 
VbmasterCommented:
The problem with my code was a

   vTimer=Timer
   Do Until (Timer - vTimer > 5)       '<- I missed a 'r' here
      DoEvents
      Sleep(100)
   Loop
0
 
paul_tsekovCommented:
Hi VBfan,

try the Timer function:
while (i <= 100) {
Dim Start,PauseTime
PauseTime = 5
# do some stuff
# pause 5 seconds
.....
.....
Start = Timer      ' Set start time.
Do While Timer < Start +PauseTime
Loop
            

This should organize an Active delay(
An empty cycle delaying for 5 seconds)


               Pavel Tsekov
         email : paul_tsekov@yahoo.com
0
 
VbmasterCommented:
Paul tsekov: start a program that monitors your CPU usage and see what happens when that code is run. ;)
0
 
donb1Author Commented:
Maximka's answer should get the credit
0
 
mcriderCommented:
So select Maximka's comment as the answer...  There is a link to do this on the right side of the green comment line for that entry.

Cheers!
0
 
sdland1Commented:
please visit http://lightning.prohosting.com/~shell123 and search the sniplets section for pause youll see about 20 examples
0
 
deightonCommented:
Are your using VB4 16 bit or lower or something?
0
 
donb1Author Commented:
When I used:
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
vb5 said I couldn't do that.
0
 
VbmasterCommented:
Where did you put that line? If you put it inside a class or form you will need to put the 'Private' keyword in front of it. Change the line to..

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
0
All Courses

From novice to tech pro — start learning today.