Some people wonder why the Sleep
function pauses the program for more
than just one millisecond even when the
dwMilliseconds
parameter is set to 1
. The reason is pretty clear, if you just consider
what the documentation actually says.
- dwMilliseconds
[in] The minimum 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 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.
A value of
INFINITE
indicates that the suspension should not time out.
The key part of that is the first paragraph. It says that the
parameter is the minimum interval. When a program calls
Sleep(1)
, it will be suspended
for at least one millisecond, but
possibly—probably—more.
Infinite wait time
The documentation says Sleep
will accept
Infinite
for its timeout. That’s not a good
idea to try because then the function will never time out. The
Infinite
timeout is provided for other, related
wait functions, such as SleepEx
, which can be interrupted before
timing out.