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.
If the service is not running, it cannot check itself.
You will need to use a second service that does the checking.
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:
Hi.
The built-in Windows Services Recovery functionality (see the screenshot in Rob's post above) will probably meet your needs. If not, I suggest trying Service Protector, which is designed to automatically keep your important Windows Services running 24/7.
Good luck!