I've got a Windows service that needs to do certain things periodically. Should I use waitable timer objects or timer queues?
What are the pros and cons of the two approaches? Is this a false dichotomy? Is there a third way?
A waitable timer was designed to activate code through an APC. That's pretty hard to get right due to re-entrancy issues and should only be considered if you need to run code on a thread that is otherwise occupied but blocks often enough to allow an APC to run.
Timer queues are very light-weight objects, their callback runs on a (cheap) thread from the thread pool. Almost always good for a periodic service.
A third way is to start a thread when the service is started and have it block with WaitForSingleObject() whose timeout sets the period. You'd wait on an event that signals that the service should stop. Very easy to get going, not as lean as a timer queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With