13

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

Ray
  • 187,153
  • 97
  • 222
  • 204

8 Answers8

17

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

Community
  • 1
  • 1
Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
  • 6
    I'm voting you up for reminding me how happy I am not to have to do VB6 anymore :-) – Matt Sep 21 '08 at 19:16
  • 1
    I'm voting you up for delivering just what this site excels in. Easy to find, pertinent and accurate advice. @Matt, I would be happy not to be using VB6 either, but my client needs it so I'm going to step up. And thanks to Kris, I didn't have to fuss around for an hour trying to remember the precise combination of 'do events' and sleep that I forgot 10 years ago. Thanks Kris, it worked like a charm! – William Oct 11 '11 at 15:50
  • For anyone still reading this a decade later, if you are having trouble getting VB6 to recognize the imported Sleep sub, try placing its declaration in a global module. Then, either use it there, or make it public so you can use it elsewhere. For whatever reason, VB6 does not like this declaration inside of class modules. It won't complain about it directly, but it will tell you that Sleep is not defined. – Daniel Jun 24 '22 at 16:04
16

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
  • 27,645
  • 10
  • 53
  • 72
2

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Sean Gough
  • 1,721
  • 3
  • 26
  • 47
1

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
RkdL
  • 112
  • 11
0

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.

0

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

EndangeredMassa
  • 17,208
  • 8
  • 55
  • 79
0

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

EndangeredMassa
  • 17,208
  • 8
  • 55
  • 79
-4

System.Threading.Thread.Sleep(500)

Mureinik
  • 297,002
  • 52
  • 306
  • 350