Best way to have a Windows service restart itself is to set up two things:
1) Go to
Services and look at the properties of your installed service. Go to
Recovery and set the
First Failure pick list to
"Restart the Service". Set the
Reset fail count after text box to 0 if you want to be able to restart it multiple times in a day. Set the
Restart service after text box to how long you want to wait. Now if your service fails for whatever reason, it will try to restart.
2) Next in your Windows service, you set it to fail. Exiting with an error code should do it. (
ExitCode ref)
Now your Windows service will tell the service provider that it failed and will restart.
Why would anyone do this, some may ask? Well performance counters is a good example if one didn't install/update them via an installation program. Performance counters need time to configure. So a Windows service can create them on-the-fly, restart, and then use them. Other than that example, I'm sure there are other reasons.
Step 1 can be replaced with running a program or even restarting the computer. I prefer to run a program that reports the error to me and then restarts the service if X number of fail counts is less than Y. I run my program in the subsequent failures.
Lastly you can configure the Recovery settings during installation of your service. There is a
CodeProject article on it.