Ray
Ray

Reputation: 192186

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

I have a long running process in VB6 that I want to finish before executing the next line of code. How can I do that? Built-in function? Can I control how long to wait?

Trivial example:

Call ExternalLongRunningProcess
Call DoOtherStuff

How do I delay 'DoOtherStuff'?

Upvotes: 14

Views: 86558

Answers (8)

RkdL
RkdL

Reputation: 112

If you want to write a sleep or wait without declaring sleep you can write up a loop that uses the systemtimer. This is what i use for testing/debugging when running the interpreter. This can be added while the interpreter is paused, if you'd need such a thing:

Dim TimeStart as currency
Dim TimeStop as currency
Dim TimePassed as currency
Dim TimeWait as currency

'use this block where you need a pause
TimeWait = 0.5 'seconds
TimeStart = Timer()
TimePassed = 0
Do while TimePassed < TimeWait  'seconds
    TimeStop = timer()
    TimePassed = TimeStop - TimeStart 
    doevents
loop

Upvotes: 1

Sean Gough
Sean Gough

Reputation: 1711

How To Determine When a Shelled Process Has Terminated:

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.

Upvotes: 2

RetroDev
RetroDev

Reputation: 1

I wish you could just add the .net framework system.dll or whatever to your project references so that you could just do this:

Dim ALongTime As Integer = 2000
System.Threading.Thread.Sleep(ALongTime)

...every time. I have VB6, and VB.net 2008 on my machine, and its always difficult for me to switch between the very different IDE's.

Upvotes: 0

twynham
twynham

Reputation: 1

System.Threading.Thread.Sleep(500)

Upvotes: -4

Kris Erickson
Kris Erickson

Reputation: 33834

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

Upvotes: 18

Nescio
Nescio

Reputation: 28403

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

Upvotes: 16

EndangeredMassa
EndangeredMassa

Reputation: 17528

Run your long-running process in the middle of your current process and wait for it to complete.

Upvotes: 0

EndangeredMassa
EndangeredMassa

Reputation: 17528

Break your code up into 2 processes. Run the first, then run your "long running process", then run the second process.

Upvotes: 0

Related Questions