Reputation: 60331
SetThreadpoolTimer
is used by my app to periodically ping a server to report my app is alive. My app records the time whenever it sends a ping and persists it to a file.
If my app is restarted it loads the last ping time. What happens if the app calls SetThreadpoolTimer
with lastPingTime + period
.
Potentially, this time is in the past!
Will SetThreadpoolTimer
catch up and fire immediately for the missed event?
If I also specify a period to SetThreadpoolTimer
, could the timer immediately fire multiple times to catch up on all the missed events?
The documentation does not explain what happens when using absolute times that are in the past.
Upvotes: 0
Views: 528
Reputation: 7170
There is MSDN sample for Thread Pool Function, and modify it like:
SYSTEMTIME sys = { 0 };
sys.wYear = 2020;
sys.wMonth = 7;
sys.wDay = 6;
sys.wHour = 8;
sys.wMinute = 0; //Suppose the current minute is 20, 0: the past time, 30: the future time.
sys.wSecond = 0;
sys.wMilliseconds = 0;
sys.wDayOfWeek = 1;
SystemTimeToFileTime(&sys,&ft);
SetThreadpoolTimer(timer,
&ft,
2000,
0);
//
// Delay for the timer to be fired
//
Sleep(60000);
The missed event on 8:00 am has been fired immediately, but it only trigger once, then trigger every 2 seconds after it.
So, the short answer is:
Will SetThreadpoolTimer catch up and fire immediately for the missed event?
Yes.
If I also specify a period to SetThreadpoolTimer, could the timer immediately fire multiple times to catch up on all the missed events?
No, it only fire once and then follow the msPeriod
to fire the event.
Upvotes: 1