Discussion Locked

VB Code

0 helpful
12 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I need code to delay the execution of the next line of code for a specific amount of time. Any ideas?

12 replies

by bill

I'm not sure exactly what you might need, but here are a couple examples from the VS6 docs to get you started:
' ---------------------------------
' Timer Event Example
' This example demonstrates a digital clock.
' To try this example, pastethe code into
' the Declarations section of a form that
' contains a Label control and a Timer
' control, and then press F5.

Private Sub Form_Load ()
Timer1.Interval = 1000 ' Set Timer interval.
End Sub

Private Sub Timer1_Timer () Label1.Caption = Time ' Update time display.
End Sub

' -------------------------------------------
This example moves a PictureBox control across a form. To try this example, paste the code into the Declarations section of a form that contains a Timer control and a PictureBox control, and then press F5. For a better visual effect you can assign a bitmap to the PictureBox using the Picture property.

Dim DeltaX, DeltaY As Integer ' Declare variables.
Private Sub Timer1_Timer () Picture1.Move Picture1.Left + DeltaX, Picture1.Top + DeltaY
If Picture1.Left < ScaleLeft Then DeltaX = 100
If Picture1.Left + Picture1.Width > ScaleWidth + ScaleLeft Then
DeltaX = -100
End If
If Picture1.Top < ScaleTop Then DeltaY = 100
If Picture1.Top + Picture1.Height > ScaleHeight + ScaleTop Then
DeltaY = -100
End If
End Sub

Private Sub Form_Load ()
Timer1.Interval = 1000 ' Set Interval.
DeltaX = 100 ' Initialize variables.
DeltaY = 100
End Sub

' ------------------

If this doesn't work for you, let me know, there are a ton of ways to accomplish what you've outlined.

-Bill

by bill

I liked "achp@mail.ru"'s idea as well, simple and easily implemented, the sleep time could easily be passed to the various sections as a variable, to vary how long certain sections wait ans so on...

by michael2

A simple method is to iterate through artificially made loops for the purpose of similating a pause. This works well because it is easy and takes up very little resources.

for i = 1 to number
do something time consuming
next i

Set "number" to something large and do something in the loop that takes some cpu cylces. Then you can tinker with either "number" or "do somethin..." to get the amount of pause. If you really want to slow things down you can nest your loops

for i = 1 to number
for j = 1 to number
do some math
next j
next i

by peyison

The following routine will delay for the specified number of seconds. It uses the VB Timer() function, which in this case is better than using a timer control because the interval on a timer control is limited to about 65 seconds.

Public Sub Delay(ByVal asSeconds As Single)
Dim sStop As Single

sStop = Timer() + asSeconds

Do Until Timer() >= sStop
DoEvents
Loop
End Sub

Hope this helps.

by achp

Do not use loops because they consume CPU time. The Sleep function from the Windows API not only does the job, but also allows other processes in the system consume CPU time:

Declare Sub Sleep Lib "Kernel32" (ByVal Milliseconds As Long)

' ...
Sleep 1000 ' Delay 1 second

by rrv

The Best API for this is SleepEx. It is self explanatory and easy to use.

All the best
RV

This discussion is closed to new replies.