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

I need to make the program sleep for about 10 seconds 1

Status
Not open for further replies.

Wings

Programmer
Feb 14, 2002
247
0
0
US
how do i induce a sleep in visual basic,
I have tried to use a for loop, and have it loop the doevents function, but if i make it loop to many times, it overflows the buffer, I just need a substitute for the c++ sleep function. Any ideas?
 

Sleep API search this site for examples using your forums last 3 months keyword "Sleep".

Good Luck

 
This is a question every vb programmer asks some time :)

Add this line to a module:

Code:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

And then use the function enywere in your program
ex:

Code:
Sleep(10000)  ' This will make app sleep for 10 seconds

 
A simpler approach may be to simply use two nested loops. I.E.,

Dim int_counter1, int_counter2 as Integer

While (counter2 <= 3) Do
While (counter1 <= 20000) Do
DoEvents
Loop
Loop

I think that's the correct syntax for While loops.
 

THOMASNG,

The code you provide is based upon CPU speed and not actual time. Meaning that on a 200 Mhz it will run 5 times longer than on a 1Ghz machine.

Good Luck

 
It is also something very different from a genuine sleep, in that it eats CPU cycles. The sleep function uses no CPU cycles
 
Having said that, I guess it is only fair to point out that, in VB's single-threaded world, a looping pause (or similar) is often more useful than sleep if you have a requirement to interrupt the pause. Of course, you'd need to put in some code such as DoEvents that defers to the OS now and again.
 
Are you using a variant for int_counter1 to slow things down?

Dim int_counter1, int_counter2 as Integer

has dimmed int_counter1 as a variant and int_counter2 as an Int
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 

Everything else said :), if you do need to pause/sleep your application it is better to do it via a small sleep in a doevents loop ...
[tt]
Dim I As Integer
For I = 1 To 100
Sleep 100 '1/10th second
Doevents
Next I
[/tt]
over a long single sleep statement...
[tt]
Sleep 10000 '10 seconds
[/tt]
Why do you ask? I'll tell you why by having you do a small experiement. Create a new project and add two buttons and the declaration for the Sleep API. Under one button put the long sleep. Under the other button use the Sleep+doevents loop. Start the task manager so it show the status of your running applications. Run the application and press the long sleep, then try to drag the form around. At this time you should also notice that the application has been reported as unresponsive in the task manager. Now repeat the process with the Sleep+Doevents loop. You will notice that you are able to drag the window around and you will not see it become unresponsive.

This concludes todays demonstration, I hope this has helped you.

Good Luck

 
The only catch is this... if you have timers that are ready to fire, they will fire when the doevents hits. I've wrestled with this delima many times. Still, VB5er's solution helps you keep control...

Excellent posts from you guys as usual.

Tuna - It's fat free until you add the Mayo!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top