VB6: How To Pause A Program

Just recently, I needed to implement a pause or delay loop in a Visual Basic 6 program.

Being lazy, my first instinct was just to do a for-next loop with a DoEvents in the middle. But this gave different results on different machines, which wasn’t what I was looking for.

Then I tried using the Sleep API call.

But more often than not, it seemed to put the whole machine to sleep, not just my program. Plus in development, it ignored me pressing Break most of the time.

Next up, a friend suggested using the timer control. Which would probably work but has a maximum delay time and I had more hassle than I’d like trying to get it to work as intended.

A web search came up with lots of people asking the same question but not many actual answers. Most of these seemed to revolve around using the Sleep API call.

Finally, I did what I should have done and wrote my own short routine.

Just send a figure in seconds to the routine and it will run in a loop until that time is up. Simple, effective and it works without hogging the whole machine.

Public Sub DelayLoop(myDelay As Double)
Dim StartTime, EndTime
StartTime = Now
EndTime = DateAdd("s", myDelay, StartTime)
Do While CDbl(Now) < CDbl(EndTime)
End Sub