Link to home
Start Free TrialLog in
Avatar of donb1
donb1

asked on

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?
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

in a  .bas

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

then
sleep(5000)   '5000 millisecs = 5secs


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
ASKER CERTIFIED SOLUTION
Avatar of Maximka
Maximka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of luthar
luthar

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 ....
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.
Avatar of donb1

ASKER

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.
The problem with my code was a

   vTimer=Timer
   Do Until (Timer - vTimer > 5)       '<- I missed a 'r' here
      DoEvents
      Sleep(100)
   Loop
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
Paul tsekov: start a program that monitors your CPU usage and see what happens when that code is run. ;)
Avatar of donb1

ASKER

Maximka's answer should get the credit
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!
please visit http://lightning.prohosting.com/~shell123 and search the sniplets section for pause youll see about 20 examples
Are your using VB4 16 bit or lower or something?
Avatar of donb1

ASKER

When I used:
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
vb5 said I couldn't do that.
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)