1

So, I have a windows service that will be started up once an hour. It will query some database tables and put data in other tables. After this is done, I would like to stop the service, that way, an hour later, it can be started again. I have tried:

this.Stop();

and

Service = new ServiceController(GlobalConstants.WindowsServiceName);
Service.Stop();
            Service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 5, 0));

When I do the first, it runs, but I get an alert that it unexpectedly stopped. There are no errors in the Event Viewer. Everything went well, it just leaves a scary alert.

When I do the second, it stalls out and says it could not start the service and hit the timeout.

If I do not have any stop and just wait for the program, the service remains in the Started state.

GOAL

To stop the service without any warning type alerts or messages. It would be nice if the OnStop() was called as well.

leppie
  • 109,129
  • 16
  • 185
  • 292
ScubaSteve
  • 6,014
  • 7
  • 44
  • 54
  • The service is supposed to leave the Started state and enter the Running state before doing any work. It is an error to stop the service while it is still in the Started state. If you stop the service *after* it has entered the Running state, that will not be considered as an error and should not generate the alert. – Harry Johnston Jan 22 '15 at 03:11
  • How do you put it into running state? – ScubaSteve Jan 22 '15 at 15:32
  • http://stackoverflow.com/questions/1095498/service-in-perpetual-starting-status – Harry Johnston Jan 22 '15 at 22:59

1 Answers1

2

You don't have to start and stop the service. Use a Timer. Set its interval to one hour and execute your process in timer elapsed event.

Define a class level Timer object. Instantiate it in OnStart and enable it. In the Elapsed event execute your process, something like:

private System.Timers.Timer serviceTimer;
protected override void OnStart(string[] args)
{
    serviceTimer = new System.Timers.Timer();
    serviceTimer.Interval = 3600000; //one hour
    serviceTimer.Elapsed += serviceTimer_Elapsed;
    serviceTimer.Enabled = true;
}

void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    serviceTimer.Enabled = false;
    // do your process
    serviceTimer.Enabled = true;
}

You can also use System.Threading.Timer, where you can specify the timer to start immediately and then execute every one hour like:

System.Threading.Timer timer = new System.Threading.Timer(
       new TimerCallback(YourMethod), 
       null, 
       0,  //initial start time
       3600000);
Community
  • 1
  • 1
Habib
  • 205,061
  • 27
  • 376
  • 407
  • That makes sense, thank you. I really like the serviceTimer.Enabled = false and true after the process is complete. – ScubaSteve Jan 21 '15 at 16:33
  • This is a reasonable workaround (though inefficient, since it leaves the process running unnecessarily) but it doesn't actually answer the question. – Harry Johnston Jan 22 '15 at 03:12
  • I think as far as using a windows service, the way Habib suggested is the way to go. Otherwise, I would probably use a scheduled task. – ScubaSteve Jan 22 '15 at 15:33