I faced a strange behavior with waitable timers. If I create it with one-second period then after the first firing, its following firings seem to be "aligned" to some millisecond value which differs significantly from first firing.
Here is example of fire times (hours:minutes:seconds.milliseconds):
18:06:25.753 <-- here 753
18:06:26.238 <-- here and later 238
18:06:27.238
18:06:28.238
18:06:29.238
If I re-run the program, the millisecond value of first firing is different, but the subsequent events happen again at 238 value.
Here is test code I used:
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
LARGE_INTEGER dueTime;
dueTime.QuadPart = 0;
SetWaitableTimer(hTimer, &dueTime, 1000, NULL, NULL, FALSE);
for (int i=0; i<10; i++)
{
WaitForSingleObject(hTimer, INFINITE);
SYSTEMTIME st;
GetLocalTime(&st);
printf("%02d:%02d:%02d.%03d\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
}
return 0;
}
I see this issue in Windows 7 and Windows Server 2008 R2, but not in Windows XP/Server 2003.
Anyone knows why this happens? I can imagine it can be some system optimization to reduce timer interrupts and/or context switches. But I can't find it documented anywhere. It is unexpected and can lead to issues.