Waitable Timers as alternative to sleep.

Hello,

My execution of the following waitable timer code did not achieve the results expected. There seems to be no

pause at all when invoked, no matter what the input is for MillisSeconds. The code used is as follows:

#define STRICT 1#define WIN32_LEAN_AND_MEAN#define _WIN32_WINDOWS 0x0500 // Not supported by 95#include <windows.h>DWORD Wait(DWORD MilliSeconds) {DWORD dw;HANDLE hTimer;LARGE_INTEGER DueTime;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 isDueTime.QuadPart = MilliSeconds * MilliSecond;hTimer = CreateWaitableTimer(NULL, TRUE, NULL);if (!SetWaitableTimer(hTimer, &DueTime, 0, NULL, NULL, FALSE)) {CloseHandle(hTimer);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 necessaryCloseHandle(hTimer);return dw;}

Thanks in advance.
[3878 byte] By [parreg] at [2008-1-9]
# 1

In SetWaitableTimer, the second parameter should contain a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). I guess you think it is the time elaspse after this function call since setting MilliSeconds*Millisecond will not likely to generate a predictable UTC time.

WeidongHuang-MSFT at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...
# 2

That is what I thought it to be. I guess this solution won't work.
parreg at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...
# 3
Whats wrong with using Sleep/SleepEx ?
RamkrishnaPawar at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...
# 4

Hello,

I need to pause about 2msecs between steps. The lowest I could get the Sleep function to was about 15 msec. I have

a delay loop, but this introduces unwanted jitter. I never tried SleepEx but I will. Thanks.

parreg at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...
# 5
Ramkrishna Pawar wrote:
Whats wrong with using Sleep/SleepEx ?
Sleep/SleepEx should not be used for timing. They're not consistent from platform to platform (they depend on the system quantum which varies between 15-30+ ms depending on the version of Windows) and the delay could be less (by almost a quantum) than the time requested and will likely be much more than the time requested as the system must decide when your thread can be given the CPU again as another thread may not have relinquished it yet.
PeterRitchie at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...
# 6
Sleep/SleepEx did/does not work. I tweaked my own SLEEP function using a loop and various statements that effectively
did nothing but cause a delay. I was able to achieve my 2ms pause and eliminate the jitter.
parreg at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...
# 7

I think the accuracy of your sleep function will be dependant on the type and speed of the processor on the system.

If you're looking for accurate timing, I suggest you switch to a message-based architecture and use timers. I know of no way to reliably block a thread for a specific amount of time with accuracy finer than 30ms increments.

PeterRitchie at 2007-10-2 > top of Msdn Tech,Software Development for Windows Vista,Windows SDK...

Software Development for Windows Vista

Site Classified