Sleep
A version of this page is also available for
4/8/2010
This function suspends the execution of the current thread for a specified interval.
Syntax
void Sleep(
DWORD dwMilliseconds
);
Parameters
dwMilliseconds
[in] Specifies the time, in milliseconds, for which to suspend execution.A value of zero causes the thread to relinquish the rest of its time slice to any other thread of equal priority that is ready to run. If no other threads of equal priority are ready to run, the function returns immediately, and the thread continues execution.
A value of INFINITE causes an infinite delay.
Return Value
None.
Remarks
A thread can relinquish the rest of its time slice by calling this function with a sleep time of zero milliseconds.
Calling Sleep(INFINITE) is different on Windows Embedded CE-based platforms than it is for Windows-based desktop platforms. For Windows Embedded CE-based platforms, Sleep(INFINITE) is equivalent to calling SuspendThread(GetCurrentThread()). This means that the thread suspend count is incremented from 0 to 1 and can be resumed, or woken up, by another thread that calls ResumeThread on the sleeping thread. A Sleep(INFINITE) call on Windows-based desktop platforms is not a SuspendThread call, and calling ResumeThread on the sleeping thread does not resume the thread.
Be careful when using Sleep and code that directly or indirectly creates windows. An example of code that indirectly creates windows is COM CoInitialize.
If a thread creates windows, it must process messages. Message broadcasts are sent to all windows in the system.
A thread that uses Sleep with an infinite delay can cause the system to deadlock. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, instead of Sleep.
Some Windows Mobile devices have an internal capacitor or backup battery to allow for swapping the main battery while the device is in sleep mode without losing any device state information.
Requirements
Header | winbase.h |
Library | coredll.lib |
Windows Embedded CE | Windows CE 1.0 and later |
Windows Mobile | Windows Mobile Version 5.0 and later |
See Also
Reference
Other Resources
CoInitialize
MsgWaitForMultipleObjects
MsgWaitForMultipleObjectsEx