parreg

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 is
DueTime.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 necessary
CloseHandle(hTimer);
return dw;
}

Thanks in advance.


Re: Windows SDK Waitable Timers as alternative to sleep.

Weidong Huang - MSFT

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.






Re: Windows SDK Waitable Timers as alternative to sleep.

parreg


That is what I thought it to be. I guess this solution won't work.




Re: Windows SDK Waitable Timers as alternative to sleep.

Ramkrishna Pawar

Whats wrong with using Sleep/SleepEx






Re: Windows SDK Waitable Timers as alternative to sleep.

parreg

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.





Re: Windows SDK Waitable Timers as alternative to sleep.

Peter Ritchie

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.




Re: Windows SDK Waitable Timers as alternative to sleep.

parreg

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.




Re: Windows SDK Waitable Timers as alternative to sleep.

Peter Ritchie

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.