Solved

How to implement a time delay in visual basic

Posted on 2000-03-09
22
1,108 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
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
 
LVL 18

Accepted Solution

by:
deighton earned 25 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now