• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1133
  • Last Modified:

How to implement a time delay in visual basic

I want to implement a time delay in visual basic AND prevent the proceeding lines of code to run until the time delay is satisfied.  The delay more or less must pause the programs proceeding code execution until that delay is done.  

Example:

run a line of code
delay time for 5 seconds
now run this line of code( this line of code will use data from the first line so it is important that this line does not run concurrently with the first line.)




0
steveyi2000
Asked:
steveyi2000
1 Solution
 
tureCommented:
steveyi2000,

How about this?

  Dim t As Date
  Debug.Print "a"
  t = DateAdd("s", 5, Now())
  Do Until Now > t: Loop
  Debug.Print "b"

Ture Magnusson
Karlstad, Sweden
0
 
JagarCommented:
Try something like this


Dim PauseTime, Start, Finish, TotalTime
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
   PauseTime = 5   ' Set duration.
   Start = Timer   ' Set start time.
   Do While Timer < Start + PauseTime
      DoEvents   ' Yield to other processes.
   Loop
   Finish = Timer   ' Set end time.
   TotalTime = Finish - Start   ' Calculate total time.
   MsgBox "Paused for " & TotalTime & " seconds"
Else
   End
End If

0
 
carruinaCommented:
With this procedure you can pause the program


Sub Pause(ByVal nSecond As Single)
DoEvents
   Dim t0 As Single
   t0 = Timer
   Do While Timer - t0 < nSecond
      'Dim dummy As Integer
      'dummy = DoEvents()
      ' if we cross midnight, back up one day
      If Timer < t0 Then
         t0 = t0 - CLng(24) * CLng(60) * CLng(60)
      End If
   Loop
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
deightonCommented:
sleep api
0
 
Erick37Commented:
Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" _
(ByVal dwMilliseconds As Long)


Call Sleep(5000)
0
 
Bob LearnedCommented:
You could use the Sleep API:

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

Call Sleep(1000)   'One second
0
 
samopalCommented:
And how about this :

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


In code
....
Sleep 5000  ' Pause 5sec
.....
0
 
TimCotteeCommented:
Even simpler, use the Sleep Api Function.

add this to declarations section of a form, or in a module declare it as Public

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

Then in code use

Sleep 5000

Code will continue after specified time.
0
 
TimCotteeCommented:
Sorry guys, tried to post this answer when there was nothing much on the thread, it finally accepted my post but you had all got there first.
0
 
wqwCommented:
hahaha, unbelievable crowd :-)))
0
 
SiriusPCommented:
easy way:

run a line of code
*enable timer1_timer
delay time for 5 seconds
*timer1_timer.interval is set to 5000
now run this line of code( this line of code will use data from the first line so it is important that this line does not run concurrently with the first line.)
write the following code in the timer sub
save the data you used in the first line which you need in the timer sub in the tag of the timer or in a global variable..
0
 
Erick37Commented:
Sleep is by far the best solution for a short pause in the code.  Using Sleep will suspend your application in a very CPU efficient wait state.

Using Timers, Time, Now, etc. will also work, but the loop is computationally expensive.
0
 
RickHulshofCommented:
This is what I use-

Public Sub timedelay()
    Dim Pausetime, Start, Finish, TotalTime
   
    Pausetime = 30  ' Set duration.
    Start = Timer   ' Set start time.
    Do While Timer < Start + Pausetime
        DoEvents
    Loop
   
End Sub

Good Luck :)
0
 
R_N_WARDCommented:
Wow, Big Thread! Room for one more?

Try this. It worked for me in many programs....

For N = 1 to 2000
For X = 1 to 2000
For Y = 1 to 2000
Next Y
Next X
Next N
0
 
R_N_WARDCommented:
Wow, Big Thread! Room for one more?

Try this. It worked for me in many programs....

For N = 1 to 2000
For X = 1 to 2000
For Y = 1 to 2000
Next Y
Next X
Next N
0
 
vbWayneCommented:
Wow,,,after reading all that I think I'll go to sleep.

:>
0
 
Glen RichmondCustomer Reporting Programmer.Commented:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

sleep(1000)

but i see now ive gone and got the info im too late, so ill just back up the usage of this as answer.....
0
 
jbathijaCommented:
implement the code

and sleep(500)
where sleep is an API call which expects the time in milliseconds
0
 
Glen RichmondCustomer Reporting Programmer.Commented:
nice one jbathija lock the question with an answer after the same answer was given ages ago..... by someone else.
0
 
steveyi2000Author Commented:
I think the sleep api is working fine.  Now the first person to answer the question was deighton, how do I assign him/her the points?
0
 
Erick37Commented:
Reject jbathija's incorrect answer and accept deighton's comment as answer.
0
 
steveyi2000Author Commented:
a little more info on api would have been nice in this answer, but you gave me enough to look it up.  thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now