0

I've inherited a Service and noticed that whenever I try to stop the service via Services, it errors and eventually shuts down, but seems very dirty. On top of that, the logging in the OnStop() event is not even firing.

When I looked at the code, the main service thread is executing periodic tasks via 2 timers. I think this is the cause of the issues, the thread must be in the middle of the timer task when the stop is requested, so it hiccups.

The pseudo code is below, what is the proper way to handle this so when I stop the service it gracefully exits?

protected override void OnStart(string[] args)
{
     _timer1 = new Timer(OneSecond * 3) {AutoReset = true};
    _timer1.Elapsed += TaskOneCode;

    _timer2 = new Timer(OneSecond * 3) {AutoReset = true};
    _timer2.Elapsed += TaskTwoCode;
}

protected override void OnStop
{
    log("Stopping...");     <---  This line does not even fire.
}
Mark Kadlec
  • 8,036
  • 17
  • 60
  • 96

0 Answers0