Reputation: 35
I have a waitable timer with a reset time of 24hours. However the timer will be delayed around 3minutes every day it's been going. So I'm wondering what could cause this. I'm using the lPeriod of SetWaitableTimer
which specifies the time to be in milliseconds. Here's the code i use to set the timer.
int SetTimr(LARGE_INTEGER* fire, node* cursor, LONG reset)
{
// Create an unnamed waitable timer.
cursor->hTimer = CreateWaitableTimer(NULL, false, NULL);
if (cursor->hTimer == NULL)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
// Set a timer to wait for N seconds.
if (!SetWaitableTimer(cursor->hTimer, fire, reset, NULL, NULL, true))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 1;
}
return 0;
}
fire
is the time when it's supposed to signal which there has never been a problem with.
reset
is the time until the timer should fire again which is set as a
constant to
#define DAY 86400000
which i understand would be exactly 24hours.
I check the timers every 250ms which shouldn't have this big effect on the delay. So I'm wondering if anyone has any thoughts on how I'm using the timers. I realize that I can just set the timer anew for the desired time but i don't understand why this doesn't work properly.
Upvotes: 0
Views: 553
Reputation: 11706
Userspace timers are general-purpose timers, and as such, are not guaranteed to be "super accurate". It's only really guaranteed that a timer will wait at least whatever interval is specified before firing.
A possible implementation might be to have a "master" timer go off every millisecond, and increment all "slave" timer counters. When a timer's counter reaches the specified interval time, it triggers the event. Depending on how the "master" timer is configured, it may take slightly longer than a millisecond between triggers (in your case, in the order of a few microseconds longer). This slight delay adds up, resulting in the 3-minute drift from your 24-hour timer.
In your case, because you want to wait an entire day between events, you might be better off periodically querying the system time, or use QueryPerformanceCounter
.
Upvotes: 2