tags:

views:

2999

answers:

5

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'?

A: 

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

EndangeredMassa
A: 

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

EndangeredMassa
+5  A: 

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
Nescio
DoEvents is what you'd use to return control to GUI during long running processes.
Sean Gough
+1  A: 

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.

Sean Gough
+3  A: 

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

Kris Erickson
milliseconds I think you meant.
Hamish Smith
Absolutely, milliseconds...
Kris Erickson
I'm voting you up for reminding me how happy I am not to have to do VB6 anymore :-)
Matt