SLEEP
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgSleep
- 最后更新: 2023-05-28
等待指定时间过去,或等待按键。
语法
vb
declare sub Sleep ( byval amount as long = -1 )
declare function Sleep ( byval amount as long , byval keyflag as long ) as long用法
Sleep [ amount [, keyflag ]]
result = Sleep ( amount, keyflag )参数
amount
等待的毫秒数(可选,默认等待按键)。
keyflag
可选标志;值为 0 表示正常休眠,值为 1 表示等待不能被按键中断。
返回值
如果 keyflag 不是有效值(即不是 0 或 1)则返回 1 表示失败,否则返回 0。
说明
Sleep 将等待直到 amount 毫秒(在 -lang qb 中可能是秒,见下文)过去(如果传递了任何值)或用户按下按键。如果 amount 小于 100 毫秒,则 Sleep 始终等待完整的请求时间(忽略按键)。
包含第二个参数 1 用于"深度"休眠,无法通过按键中断。
Sleep 的精度因操作系统周期时间而异:
Windows NT/2K/XP:15 毫秒,9x/Me:50 毫秒,Linux:10 毫秒,DOS:55 毫秒。
对于 amount 参数使用低于这些精度值的值,不允许 Sleep 产生相应的等待值(始终更高的等待,约为这些值的量级),但 amount 值 '0' 除外。
在等待用户输入或在线程内循环时,以 25 毫秒或更短的时间调用 Sleep 以释放时间片。这将防止程序不必要地占用 CPU。
Sleep 不会清除键盘输入缓冲区,在调用 Sleep 期间按下的任何键都会被保留,稍后可以通过 Inkey、Getkey 或 Input 读取。
当 Sleep 没有参数(仅等待按键)时,可以使用 Getkey 关键字代替 Sleep。
对于 Sleep 的一般形式(带参数),如果用户想要清除键盘输入缓冲区中在 Sleep 执行期间可能按下的任何键,可以在 Sleep 指令行后使用如下方法:
start GeSHi
While Inkey <> "": Wend '' loop until the keyboard input buffer is emptyend GeSHi
示例
start GeSHi
vb
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 500end GeSHi
start GeSHi
vb
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 & "'"
Sleepend GeSHi
方言差异
- 在 -lang fb 和 -lang fblite 方言中,
amount值以毫秒为单位。 - 在 -lang qb 方言中,
amount值与 QB 一样以秒为单位。如果给出第二个参数keyflag,或关键字写为__Sleep,则值预期以毫秒为单位。
与 QB 的区别
- 在 -lang qb 方言中无区别。
- 在 QB 中,延迟只以整秒给出,不支持
keyflag参数。
另请参阅
TimerInkey
返回 目录