?
Solved

How to implement a time delay in visual basic

Posted on 2000-03-09
22
Medium Priority
?
1,118 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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