Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to implement a time delay in visual basic

Posted on 2000-03-09
22
Medium Priority
?
1,125 Views
Last Modified: 2013-11-25
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
Comment
Question by:steveyi2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
22 Comments
 
LVL 22

Expert Comment

by:ture
ID: 2601207
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
 
LVL 2

Expert Comment

by:Jagar
ID: 2601217
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
 
LVL 3

Expert Comment

by:carruina
ID: 2601237
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!

 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 2601245
sleep api
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2601246
Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" _
(ByVal dwMilliseconds As Long)


Call Sleep(5000)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 2601249
You could use the Sleep API:

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

Call Sleep(1000)   'One second
0
 
LVL 9

Expert Comment

by:samopal
ID: 2601252
And how about this :

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


In code
....
Sleep 5000  ' Pause 5sec
.....
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2601255
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 2601261
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
 
LVL 4

Expert Comment

by:wqw
ID: 2601371
hahaha, unbelievable crowd :-)))
0
 
LVL 1

Expert Comment

by:SiriusP
ID: 2601476
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2601598
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
 
LVL 3

Expert Comment

by:RickHulshof
ID: 2601659
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
 

Expert Comment

by:R_N_WARD
ID: 2601845
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
 

Expert Comment

by:R_N_WARD
ID: 2601851
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
 
LVL 1

Expert Comment

by:vbWayne
ID: 2601944
Wow,,,after reading all that I think I'll go to sleep.

:>
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 2602761
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
 
LVL 1

Expert Comment

by:jbathija
ID: 2604460
implement the code

and sleep(500)
where sleep is an API call which expects the time in milliseconds
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 2604512
nice one jbathija lock the question with an answer after the same answer was given ages ago..... by someone else.
0
 

Author Comment

by:steveyi2000
ID: 2604548
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2604752
Reject jbathija's incorrect answer and accept deighton's comment as answer.
0
 

Author Comment

by:steveyi2000
ID: 2604794
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question