Using Waitable Timers

Click here to change the theme.

The following should be a reasonable alternative to use of Sleep().

#define STRICT 1
#define _WIN32_WINDOWS 0x0500 // Not supported by 95
#include <windows.h>

DWORD Wait(DWORD MilliSeconds) {
	DWORD dw;
	HANDLE hTimer;
	LONGLONG MilliSecond=-10000;
if (_winmajor == 1 && _winminor <= 4)	// Win95 or less?
	return WAIT_FAILED;		// This is not best since WAIT_FAILED implies
		// that GetLastError will say what the problem is
DueTime.QuadPart = MilliSeconds * MilliSecond;
hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
if (!SetWaitableTimer(hTimer, &DueTime, 0, NULL, NULL, FALSE)) {
	return WAIT_FAILED; // This needs to be improved
	// A more meaningful return value should be retuned; A BOOL
	// return value would be good and upon failure the caller could
	// call GetLastError
dw = WaitForSingleObject(hTimer, 10000);	// A maximum of 10 seconds?
				// Adjust if necessary
return dw;

Hosted by