You are here: Visual
Basic > VB6
(Beginners Tutorial)
Tutorial
Main Page | Previous Page | Contents
| Next Page
Using GetTickCount to Implement a Delay
Many times, you want some delay in a program. We can use GetTickCount
to form a user routine to implement such a delay. We’ll write a quick example
that delays two seconds between beeps.
-
Start a new project. Put a command button on the form. Copy and paste the proper
Declare statement.
-
Use this for the Command1_Click event:
Private Sub Command1_Click()
Beep
Call Delay(2#)
Beep
End Sub
-
Add the routine to implement the delay. The routine I use is:
Private Sub Delay(DelaySeconds As Single)
Dim T1 As Long
T1 = GetTickCount()
Do While GetTickCount() - T1 < CLng(DelaySeconds * 1000)
Loop
End Sub
To use this routine, note you simply call it with the desired delay (in seconds)
as the argument. This example delays two seconds. One drawback to this routine
is that the application cannot be interrupted and no other events can be processed
while in the Do loop. So, keep delays to small values.
-
Run the example. Click on the command button. Note the delay between beeps.
Tutorial
Main Page | Previous Page | Contents
| Next Page
|