How do I delay code execution in Visual Basic (VB6)?

While Nescio's answer (DoEvents) will work, it will cause your application to use 100% of one CPU. Sleep will make the UI unresponsive. What you need is a combination of the two, and the magic combination that seems to work best is:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

While IsStillWaitingForSomething()
    DoEvents
    DoEvents
    Sleep(55)
Wend

Why two DoEvents, and one sleep for 55 milliseconds? The sleep of 55 milliseconds is the smallest slice that VB6 can handle, and using two DoEvents is sometimes required in instances when super-responsiveness is needed (not by the API, but if you application is responding to outside events, SendMessage, Interupts, etc).


VB.Net: I would use a WaitOne event handle.

VB 6.0: I've seen a DoEvents Loop.

Do
     If isSomeCheckCondition() Then Exit Do
     DoEvents
Loop

Finally, You could just sleep:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 10000

How To Determine When a Shelled Process Has Terminated:

  • Archive 1

  • Archive 2

If you're calling an external process then you are, in effect, calling it asynchronously. Refer to the above MS Support document for how to wait until your external process is complete.

Tags:

Vb6

Process