I have a Windows service class deriving from ServiceBase
that uses a System.Timers.Timer
to run code at frequent intervals. The handlers for OnStop
and OnPause
use some signalling with the timer thread to check if the timer is still running and waits for it to finish.
Is there a recommended way of handling delay errors in this situation, such as if the handler is waiting for an unacceptably long time for the timer thread to stop?
Should I just continue waiting until the SCM gives up (but that will put the service into a state where you can't do anything with the service except kill the process, and sometimes a reboot is the only way to restart it)? Or I could throw an exception perhaps (would this leave it in a similar state, or just abort the stop/pause request)?
I'd prefer it if I can reject the request to pause/stop and have the SCM leave it in a state where the user can try to pause/stop again. I could abort the timer thread but sometimes it's locked on other resources that I'd rather it would wait until completion, and just have an error logged with the administrator able to attempt a stop later.