# How to create delay in Visual Basic?

Status
Not open for further replies.

#### bb

##### Newbie level 6
visual basic delay

How to create delay in Visual Basic? thx 4 help..

Private Sub Command1_Click()

dim i as Integer
For i = 1 To 1000
Next i
Picture1.Print 1

End Sub

does the program above create any delay before printing number 1?

#### cfant

##### Full Member level 3
vb delay

Yes, but you will have hard time trying to see it. Using a empy loop is bad way to introduce delay in any program. See this link for better description:

https://support.microsoft.com/kb/q212667/#XSLTH3125121123120121120120

Or use this:

Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Sub SlowDown(MilliSeconds As Long)

Dim lngTickStore As Long

lngTickStore = GetTickCount()

Do While lngTickStore + MilliSeconds > GetTickCount()
DoEvents
Loop

End Sub

delay vb

#### Cortex

##### Full Member level 3
delay visual basic

Sleep (1000) // pause 1 s

#### jhbbunch

##### Full Member level 4
delay in vb

I used the Win32 API functions QueryPerformanceCounter() and QueryPerformanceFrequency() to get time intervals as low as 3 microseconds some time ago. I helped a student drive two stepper motors with it with perfect accuracy. But use them in a loop that has DoEvents() as stated above. Also, you can get the odd glitch because of the preemptive nature of a multitasking system when time intervals get quite small.

#### algilsan

delay in visual basic

Another way to do the same

Public Sub Delay(tiempo As Integer)

salir = False
Timer1.Interval = tiempo
Timer1.Enabled = True
Do While (Not salir)

DoEvents

Loop
Timer1.Enabled = False

End Sub

#### ibrahim03

##### Full Member level 1
delay en visual basic

I dont know about vb4 but I use vb 6 and it has timers. you just have to add a timer object to your form and set the interval of the timer. In this way you can get a delay from 1ms to abt 3 seconds (depending on the value of the interval). If vb4 has timers then it is fairly easy to create delays !!

#### nav_vlsi

visual basic delay function

timer is a easy way to implement delays in VB

#### AlexOrtega

##### Newbie level 1
I use this code to calculate very tiny delays

Dim t1, t2, lapse As Double

lapse = 1000000 ' Use a large value or you will get a "divide by 0" error

t1 = Timer
For cont = 1 To lapse
Next
t2 = Timer

Text1 = "Seconds used: " & t2 - t1
Text2 = "In one second the instruction For executes about " & lapse / (t2 - t1) & " times"

As example, in my computer, For is executed about 42,000,000 in one second

Last edited:

#### spudboy488

##### Full Member level 5
Code:
    Public Sub Wait(ByVal mS As Integer, Optional ByVal CheckEvents As Boolean = False)

Dim Start As Long = Now.Ticks

Do
If CheckEvents Then Application.DoEvents()
Loop Until Now.Ticks > Start + (mS * TimeSpan.TicksPerMillisecond)

End Sub

Status
Not open for further replies.