Visual Basic Programming Code Examples
Visual Basic > API and Miscellaneous Code Examples
How to program a delay using the Timer function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
How to program a delay using the Timer function.
' You can delay execution of your code for a specific time interval
' by using the Timer function. Increments such as .25 or .5 can be
' used as well.
' To use the Timer function to pause for a number of seconds,
' store the value of Timer in a variable. Then use a loop to wait
' until the Timer returns a specified number of seconds greater than
' the stored value. If the delay loop will execute when midnight
' passes, compensate by reducing the starting Timer value by the
' number of seconds in a day (24 hours * 60 minutes * 60 seconds).
' Calling DoEvents from within the loop allows events to be
' processed during the delay.
' Drop this sub in the appropriate form:
Sub Pause (ByVal nSecond As Single)
Dim t0 As Single
Dim dummy As Integer
t0 = Timer
Do While Timer - t0 < nSecond
dummy = DoEvents()
' If we cross midnight, back up one day
If Timer < t0 Then
t0 = t0 - 24 * 60 * 60 ' or t0 = t0 - 86400
End If
Loop
End Sub
' Call the routine from the appropriate event:
' Example:
Call Pause(2) ' delay for 2 seconds
Form2.Show