Microsoft KB Archive/96069

= How to Program a Delay Using the Timer Function =

Article ID: 96069

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic for MS-DOS

-



This article was previously published under Q96069



SUMMARY
You can delay execution of your code for a specific time interval by using the Timer function.

With Visual Basic for MS-DOS, you cannot use the SLEEP statement to do this while forms are showing. An attempt to do so causes this error:

Invalid when forms are showing.

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 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.



Code Example
Sub Form_Click Print &quot;hello &quot;; Call Pause(2) ' delay for 2 seconds Print &quot;world&quot; End Sub

Sub Pause (ByVal nSecond As Single) 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

Additional query words: B_VBasic B_VBMSDOS 1.00 2.00 3.00 wait

Keywords: KB96069

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.