Suspends the execution of the current thread until the time-out interval elapses.
To enter an alertable wait state, use the
SleepEx
function.
Syntax
VOID WINAPI Sleep(
__in DWORD dwMilliseconds
);
Parameters
dwMilliseconds
[in]
The time interval for which execution is to be suspended, in milliseconds.
A value of zero causes the thread to relinquish the remainder of its
time slice to any other thread that is ready to run. If there are no
other threads ready to run, the function returns immediately, and the
thread continues execution.
Windows XP/2000:
A
value of zero causes the thread to relinquish the remainder of its time
slice to any other thread of equal priority that is ready to run. If
there are no other threads of equal priority ready to run, the function
returns immediately, and the thread continues execution. This behavior
changed starting with Windows Server 2003.
A value of INFINITE indicates that the suspension should not time out.
Return Value
This function does not return a value.
Remarks
This function causes a thread to relinquish the remainder of its time
slice and become unrunnable for an interval based on the value of dwMilliseconds
. The system clock "ticks" at a constant rate. If dwMilliseconds
is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds
is greater than one tick but less than two, the wait can be anywhere
between one and two ticks, and so on. To increase the accuracy of the
sleep interval, call the timeGetDevCaps
function to determine the supported minimum timer resolution and the timeBeginPeriod
function to set the timer resolution to its minimum. Use caution when calling timeBeginPeriod
, as frequent calls can significantly affect the system clock, system power usage, and the scheduler. If you call timeBeginPeriod
, call it one time early in the application and be sure to call the timeEndPeriod
function at the very end of the application.
After the sleep interval has passed, the thread is ready to run. If
you specify 0 milliseconds, the thread will relinquish the remainder of
its time slice but remain ready. Note that a ready thread is not
guaranteed to run immediately. Consequently, the thread may not run
until some time after the sleep interval elapses. For more information,
see
Scheduling Priorities
.
Be careful when using Sleep
in the following scenarios:
- Code that directly or indirectly creates windows (for example, DDE and COM CoInitialize
).
If a thread creates any windows, it must process messages. Message
broadcasts are sent to all windows in the system. If you have a thread
that uses
Sleep
with infinite delay, the system will deadlock.
- Threads that are under concurrency control. For example, an I/O
completion port or thread pool limits the number of associated threads
that can run. If the maximum number of threads is already running, no
additional associated thread can run until a running thread finishes. If
a thread uses Sleep
with an interval of zero to wait
for one of the additional associated threads to accomplish some work,
the process might deadlock.
For these scenarios, use
MsgWaitForMultipleObjects
or
MsgWaitForMultipleObjectsEx
, rather than
Sleep
.
Examples
For an example, see
Using Thread Local Storage
.
Requirements
Minimum supported client
Windows 2000 Professional |
Minimum supported server
Windows 2000 Server |
Header
Winbase.h (include Windows.h) |
Library
Kernel32.lib |
DLL
Kernel32.dll |
See Also
MsgWaitForMultipleObjects
MsgWaitForMultipleObjectsEx
Process and Thread Functions
SleepEx
Suspending Thread Execution
Threads
分享到:
相关推荐
sleepfunction.R
sleep Function Section 10.20. Job-Control Signals Section 10.21. Additional Features Section 10.22. Summary Exercises Chapter 11. Threads Section 11.1. Introduction ...
p&mJRJz VOL.30 I4 JULY 1983 COMMENTARY IIIThe function o f dream sleep Francis Crick* & Graemi M itchison*We propose that the function of dream sleep (more proper& rapid-&ye movement or REM sleep) LT...
它将阻塞其周围上下文中的所有其他内容,直到设置完成标志:调用 done() 将上下文程序流恢复到正常的 asyc安装$ npm install stall用 var stall = require ( 'stall' )//template for a sleep functionvar sleep = ...
前言 此教程是在vue中引入nosleep.js,并使用。 安装 nosleep.js npm install nosleep.js 引入 在main.js中引入 import NoSleep from nosleep.js/dist/NoSleep.min.js; Vue.prototype.$NoSleep ... function enableN
本文实例讲述了python中sleep函数用法。分享给大家供大家参考。具体如下: Python中的sleep用来暂停线程执行,单位为秒 #----------------------------------- ...# function. #-----------------
如何处理:SM12 用户登入不上WEB界面,表里头提示“SLEEP”.pdf Use Function Module 'Enque_Sleep'
sleep和msleep功能可以这样实现: function msleep ( n ) { Atomics . wait ( new Int32Array ( new SharedArrayBuffer ( 4 ) ) , 0 , 0 , n ) ;}function sleep ( n ) { msleep ( n * 1000 ) ;} 如果您需要usleep...
但是,这两个函数是异步的,在计时的过程中它们后面的代码还是会继续执行。...function sleep(sleepTime) { for(var start = Date.now(); Date.now() – start <= sleepTime; ) { } }sleep(500
sleep函数延时函数:等待一段时间,再继续进行async function output() {let out = await sleep(1000);参考实
安装: npm install function-repeat用法:重复(val,n) var repeat = require ( 'function-repeat' ) ;var res = repeat ( 1 , 4 ) ; // -> [1, 1, 1, 1]var res = repeat ( 'a' , 4 ) ; // -> ['a', 'a', 'a', '...
javascript中不提供sleep功能,而... 代码如下: function sleep(n) { var start=new Date().getTime(); while(true) if(new Date().getTime()-start>n) break; } 当然这个方法依然是靠空转CPU的方法。 还有一个方法是使
睡眠-es6 诺言入睡安装npm install sleep-es6 --saveoryarn add sleep-es6用法import { sleep } from "sleep-es6" ;...原料药function sleep ( timeout : number ) : Promise < void> ;测试npm installnpm test
有几种方式,但都不完美 其一,不断循环,直到达到指定时间 代码如下: function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; while (true) { now = new Date(); if ...
等待睡眠异步/等待简单睡眠安装npm install --save ...async function myAsyncFunction ( ) { console . time ( 'Sleeping' ) ; await sleep ( 1500 ) ; console . timeEnd ( 'Sleeping' ) ; // Sleeping: 1507.180ms}
sleepMode ( function ( err , stderr , stdout ) {if ( ! err && ! stderr ) {console . log ( stdout ) ;}} ) ;命令行界面$ npm install --global sleep-mode$ sleep-mode执照麻省理工学院:copyright:
var sleep = function (ms, cb) { setTimeout(function () { cb(); }, ms); }; sleep(20, function () { console.log(0); sleep(20, function () { console.log(1); sleep(20, function () { console.log(2...
最近在写一些奇怪的东西的时候,发现大佬们用go或者其他语言实现的并发任务用了thread.sleep让主进程暂停。 回头一想,妈个鸡我要复制粘贴到node一直循环不合适啊,我也需要暂停来着!...function slee
async function test ( ) { console . log ( 1 ) ; await sleep ( 3000 ) ; // 3000 ms delay console . log ( 2 ) ; // Output 2 after about 3000 milliseconds } 线程睡眠 真正的事件循环可阻止睡眠。 在主线...