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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the best way to delay or set a timer?

Status
Not open for further replies.

qedusa

Programmer
Jan 12, 2002
43
US
I want to set some kind of delay or timer between doing something, for example:

// Do something

// Now wait for half a second

// Now do something else

What's the best way to do this?
 
Using:

Sleep(50000); // parameter is in milliseconds

is much superior to a for/next loop, since sleep allows the processor to move on to other tasks (system or other programs), whereas a for/next loop hogs the processor for no good reason.
 
Hi

The drawback of using Sleep() is that your program stops receiving Windows messages and looks like a dead application.
Using a timer is a better solution. You create the timer and then use the handle OnTimer of update counter that will trigger the 'Do Something' and a little bit later the 'Do Something else'

HTH

Thierry
 
Actually that's true. A trigger can be better when you're waiting for a specific event, rather than just waiting an arbitrary amount of time...

However, Sleep() is often used in multi-threaded applications to allow the other threads to have processor time. These are usually "background tasks", where the user has no awareness of any short delay.

It should be used sparingly (at least for very short periods of time) in a single threaded "User Interface" application, since it *does* make the app look like it's dead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top