I need to check whether my window service is running or not every 15 minutes or so.
If it is not running, then how can I restart the windows service again?
You can check if a service is running with a ServiceController:
ServiceController sc = new ServiceController("servicename");
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
// Start the service if the current status is stopped.
sc.Start();
}
Of course, you will need to call this from another service, or create it as a small program which you then can schedule to run every 15 minutes or so.
You don't need an extra process to recover your service:
If you want to be certain that your windows service is always running, check its properties in the Recovery tab. Set all failure actions to "Restart the Service" and set "Restart service after" to 0 minutes. The moment your service disappears it will be restarted immediately. Increase the timeout if it's ok to wait a bit longer before a restart is done.
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