We help IT Professionals succeed at work.

Need help with a command

Linky
Linky asked
on
Medium Priority
157 Views
Last Modified: 2010-05-02
In QBASIC there is a simple command called 'SLEEP' and it makes the program sleep for a given amount of time. How do you do that in Visual Basic?
Comment
Watch Question

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

Call Sleep(1000)  '1 seconds

Cheers,,T
Commented:
The above should work.  You could also set a timer:

Timer1.Interval = TimeInMilliseconds
Timer1.Enabled = true

...
Private Sub Timer1()
   Timer1.Enabled=false
   msgbox "Time is up!", vbOKOnly or vbInformation
End Sub

To include this in a routine somewhere, you're better off using the API sleep function, but you could also do this:

...
Timer1.Interval = 2000 ' wait two seconds
While Timer1.Enabled
   DoEvents
Wend
' Timer has now gone off and the rest of the code is ready.

---
The above will technically not sleep, but keeps processing until the timer goes off and then is disabled.  In essence, that's what the sleep command does, but it does it in a more hidden way.  This could probably be emulated like this:

...
' some code here
MySleep 2
' code after the sleep
...

Private Sub MySleep(TimeInSeconds as single)
   Timer1.Inteval = TimeInSeconds * 1000
   Timer1.Enabled = True
   While Timer1.Enabled
      ' Wait until timer goes off and disables itself
      ' Meanwhile, let Windows functions continue
      DoEvents
   Wend
End Sub
Just a point to note the VB timer control is not very accurate so if you want to use it for anything other than setting a timer for a few seconds it's ok.

However it has an upper limit of 65,535 milliseconds or just over a minute.

The API call Sleep is usually a better option for this.

Vin.

Commented:
yes, given the question, the sleep API call is probably the best, most accurate choice.

Commented:
Sorry I'm confused as to the answer chosen ?

Linky can you comment on your choice please.

Thanks,,T.

Commented:
Actually, so am I.  I think lyonst had the best answer in this case...unless we all missed something in the request.  Actually, I think the timer is a good choice since it's a VB component rather than an API, but an explanation of that would have been nice...
Have to agree with rspahitz
I think you have made a mistake Linky.

Vin.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.