{{fbdoc item="title" value="SLEEP"}}----
Waits until a specified time has elapsed, or a key is pressed.

{{fbdoc item="syntax"}}##
	[[KeyPgDeclare|declare]] [[KeyPgSub|sub]] **Sleep** ( [[KeyPgByval|byval]] //amount// [[KeyPgAs|as]] [[KeyPgLong|long]] = -1 )
	[[KeyPgDeclare|declare]] [[KeyPgFunction|function]] **Sleep** ( [[KeyPgByval|byval]] //amount// [[KeyPgAs|as]] [[KeyPgLong|long]] , [[KeyPgByval|byval]] //keyflag// [[KeyPgAs|as]] [[KeyPgLong|long]] ) [[KeyPgAs|as]] [[KeyPgLong|long]]
##
{{fbdoc item="usage"}}##
	**Sleep** [ //amount// [, //keyflag// ]]
	//result// = **Sleep** ( //amount//, //keyflag// )
##
{{fbdoc item="param"}}
	##//amount//##
		Optional number of milliseconds to wait (default is to wait for a key press).
	##//keyflag//##
		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.

{{fbdoc item="ret"}}
	Returns ##1## if ##//keyflag//## was not a valid value (i.e. something other than ##0## or ##1##) to indicate failure, or ##0## otherwise.

{{fbdoc item="desc"}}
	##**Sleep**## will wait until ##//amount//## milliseconds (can be seconds in //[[CompilerOptlang|-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**## will always wait the full requested amount (key presses are ignored).
	
	Include the second 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.
	And using for the ##//amount//## parameter values lower than these accuracy values does not allow ##**Sleep**## to produce the corresponding wait values (always higher wait, and of the order of these values) except for the ##//amount//## value '0'.
	
	Call ##**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 retained and can be later read by ##[[KeyPgInkey|Inkey]]## or ##[[KeyPgGetkey|Getkey]]## or ##[[KeyPgInput|Input]]##.
	When ##**Sleep**## has no parameters (waiting for a key pressed only), ##[[KeyPgGetkey|Getkey]]## keyword can be used instead of Sleep.
	For the general form of ##**Sleep**## (with parameters), if the user want to clear the keyboard input buffer from any eventual keys pressed during the ##**Sleep**## execution, he can use after the ##**Sleep**## instruction line something like the following method:
		%%(freebasic)
While Inkey <> "": Wend  '' loop until the keyboard input buffer is empty
		%%
{{fbdoc item="ex"}}
	{{fbdoc item="filename" value="examples/manual/system/sleep.bas"}}%%(freebasic)
Print "press a key"
Sleep
GetKey  '' clear the keyboard input buffer, and even in that code case, the 'Sleep' keyword can be outright omitted
Print "waiting half second"
Sleep 500
	%%
	{{fbdoc item="filename" value="examples/manual/system/sleep2.bas"}}%%(freebasic)
Dim As String s

Print "wait 3 seconds or press a key"
Sleep 3000
Print "outputed by timeout or key pressed"
While Inkey <> ""  '' loop until the keyboard input buffer is empty
Wend

Input "enter a string"; s
Print "string entered: " & "'" & s & "'"

Sleep
	%%
{{fbdoc item="lang"}}
	- In the //[[CompilerOptlang|-lang fb]]// and //[[CompilerOptlang|-lang fblite]]// dialects, the ##//amount//## value is in **milliseconds**.
	- In the //[[CompilerOptlang|-lang qb]]// dialect, the ##//amount//## value is in **seconds** as in QB. If the second parameter ##//keyflag//## is given, or the keyword is written as ##**""__Sleep""**##  the value is expected to be in **milliseconds**.

{{fbdoc item="diff"}}
	- None in the //[[CompilerOptlang|-lang qb]]// dialect.
	- In QB, the delay was given in whole seconds only and did not support the ##//keyflag//## parameter.

{{fbdoc item="see"}}
	- ##[[KeyPgTimer|Timer]]##
	- ##[[KeyPgInkey|Inkey]]##

{{fbdoc item="back" value="CatPgMisc|Miscellaneous"}}