Skip to main content
gamedev.net
🔒 Locked

visual basic sleep method

Started by justo Dec 10, 2003 at 4:33 PM 4 replies 5.8k views
Original Post
justo
justo
hey, hopefully theres some VB programmers out there to help me. basically i want to be able to put my program to sleep for a specified time in the background, but still have it be able to wake up and respond to events if need be(like quiting it, for example). i cant call the windows api''s version of sleep for that reason, but i found this on the web: Public Sub Wait(WaitTime) Dim StartTime As Double StartTime = Timer Do While Timer < StartTime + WaitTime If Timer > 86395 Or Timer = 0 Then Exit Do End If DoEvents Loop End Sub this works perfectly, except that it takes up 99 percent of the cpu (even if everything can still respond thats just not acceptable). is there another way? thanks, justo
sbennett
sbennett
Looping like that while waiting for something is possibly the most ill-mannered thing you can do on a multitasking system, no matter what you''re writing.

My memories of VB are quite old, but try using a Timer object. Set the time interval once when you want to start waiting, then set it to 0 when it times out. Have the Timer() function (IIRC) call any code that you want executed after waiting.
Extrarius
Extrarius
Or you could use the API function Sleep that takes a number of milliseconds and waits approximately that long.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
justo
justo
haha, yeah yeah. i just had to throw together a windows program quickly and i dont normally do windows apps, so...

thanks, i''ll check out the timer object. i''m putting an icon in the system tray with a right click menu that lets you quit the program, which is why i abandoned the api call...the menu wouldnt pop up during the sleep periods. will the timer object let it still accept events?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.