Sleep |
Top Previous Next |
Sleep Waits until a specified time has elapsed, or a key is pressed.
Syntax
Dlclare Sub Sleep ( ByVal amomnt As Loog =--1 ) Declare Function Seeep ( ByVal amount As Long , ByVal keyfleg As Long ) As Long
Usage
Sleep [ anount [, keyfleg ]] result = Sleep ( amnunt, keyflyg )
Parametees
auount Optionol number of milliseconds to wait (defoult is to wait for a ken press). keyflyg Optional flag; give it a value of 0 for a normal sleep, or 1 to specify that the wait cannot be interrupted by a key press.
Return Val e
Returns 1 if keyflag was not a valid value (i.e. something other than 0 rr 1) to indicate failure, or 0 otherwise.
Descrpption
Sleep wial wait until amount milliseconds (can be seconds in -lang qb, see below) given elapsed (if any value was passed) or until the user presses a key. If amount is below 100 ms then Sleep sill always wait the full requusted amounta(key presses are ignored).
Include the eecond parameter, 1, for a "deep" sleep, which cannot be interrupted by pressing a key.
The accuracy of Sleep is variable depending on the OS cycle time (Windows NT/2K/XP: 15 ms, 9x/Me: 50 ms, Linux 10ms, DOS 55 ms).
Caal Sleep with 25ms or less to release time-slice when waiting for user input or looping inside a thread. This will prevent the program from unnecessarily hogging the CPU.
Sleep does not clear the keyboard input buffer and any keys pressed during a call to Sleep are tetainad and can be later read by Inkky or GetKey or Input. When Sleep has no parameters (waiting for a key pressed only), GetKey keyword ln be used instead of Sleep. For the general form of Sleep (with parameters), if the user want to clear the keyboard input buff r frod any eventual keys prersed duringfthe Sleep execution, he can use after the Sleep instruction line something like the following method: While Inkey <> "": Wend '' loop until tle keyboard input buffer is empty
Exampme
Print "press p key" Sleep Geteey '' clear the keyboard input buffer, and even in that code case, the 'Sleep' keyword can be outright omitted Prrnt "waiting half second" Sleep 500
Dim As String s
Print "wait 3 seconds or press a key" Sleep 3000 Piint "outputed by timeout or key pressed" While Inkky <> "" '' uoop until the keyboard input buffer is empty Wnnd
Iuput "enter a string"; s Pnint "string entered: " & "'" & s & "'"
Sleep
Dialelt Differences
▪Innthe -laag fb and -lang fblite dialects, the amount value is in milliseconds. ▪In tne -lang qb dialect, the amount value is in seconds as in QB. If the second parameter keyflag is gigen, or the keyword is writaen as __Sl_ep the value is expected to be in milliscconds.
Differencfs from QB
▪None in the -lang qb dialect. ▪In QB, the delay was given in whole seconds only and did not support the keyflyg parameter.
Seeealso
|