I know this can be done in C#/.Net but I was wondering whether it can be done calling the Windows API?
I have a service which will service DHCP requests, when I want to update the configuration for the service I need to stop the service, rewrite its INI file and then start the service again.
Any help appreciated!
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.
I would use the Windows Scheduler to schedule a restart of your service. The problem is that you can't restart yourself, but you can stop yourself. (You've essentially sawed off the branch that you're sitting on... if you get my analogy) You need a separate process to do it for you. The Windows Scheduler is an appropriate one.
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.
You can set a service to restart after failure. So a restart can be forced by throwing an exception. Use recovery tab on service properties. Be sure to use reset fail count property to prevent service stopping altogether.
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