Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to wait?

Status
Not open for further replies.

sdillard

Technical User
Oct 23, 2001
32
US
My app is supposed to start tasks based on a schedule which can be irregular. Right now, it waits by running a Do loop until the start time is >= to the system time. It is possible that it could sit in this loop for hours. Is this a huge resource drain? Is there a better way to do this?
 
I can think of two possibilities. One is to use Sleep() as in:

Private Declare Sub Sleep Lib "kernel32" ( _
ByVal dwMilliseconds As Long)
...
...your loop...
Sleep 60000
...your loop...
...

This will wait one minute inside your loop each time through.

A better solution is probably to use a VB Timer control depending on what else you are doing. The maximum Interval for a Timer is 65,535 milliseconds, and I wouldn't be surprised to find that Sleep() has the same upper limit.

Note that if you use Sleep() in this way your program will appear "locked up" while sleeping, and can't even repaint itself as other windows cover it and then expose it, etc.

A better compromise might be to put in something like:

...your loop...
DoEvents
Sleep 1000
...your loop...

This frees a LOT of CPU cycles but keeps your application responsive to minimize/restore, repaint/resize, and so on.

Using both like this leaves me we a nagging feeling that smething isn't right, but it works just fine for me.

But as I said, using a Timer control is best in most cases, coding the Timer event handler to check if it is time to "do" something.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top