Click here to Skip to main content
15,867,997 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I created a new Windows Service project which, when running, needs to be restarted after every time it updated it's own resources (DLL's etc).

Can I get my Windows Service to restart itself? 

I really hope that it won't be necessary to create a second application/service to do the restart. 

Regards 

Posted

Have you tried:

C#
Application.Restart();

I know it will cause a normal winforms app to terminate and restart, but I'm not sure if it'll work for a Windows service. Something to try though...

 
Share this answer
 
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.
 
Share this answer
 
Someone here suggested scheduling a task to have the service start (say, in 30 seconds in the future), then have the service stop itself.
 
Share this answer
 

As far as I know, it is not possible to restart the process from the same process. You need to take the help of some other service or external process to do this job.

For instance, you might take the help of Scheduler service / a Batch file etc.

Say you create a batch file, and place :

net stop "yourservicename"
net start "yourservicename" 

Your service will be restarted. Remember to run a new process from the existing service. Dont pass this command from code.

 
Share this answer
 
You can also set the recovery attributes using a shell cmd.exe:

VB
Private Sub ProjectInstaller_Committed(sender As Object, e As System.Configuration.Install.InstallEventArgs) Handles Me.Committed
      Try
               Shell("cmd.exe /c sc failure YourServiceName reset= 0 actions= restart/60000/restart/60000/restart/60000", AppWinStyle.Hide)
      Catch ex As Exception
      End Try
End Sub

Credit for this solution goes to Mike Trank @ http://www.codeproject.com/Messages/4010920/An-Easier-Way.aspx[^]
 
Share this answer
 
v2
Comments
fjdiewornncalwe 11-Apr-12 10:23am    
Please don't answer questions that are 2 years old, especially if the OP has selected a correct answer already. Cheers.
MatthysDT 11-Apr-12 10:37am    
Hi rgonzruiz, thanks, that's actually quite useful and I think it's relevant to the topic, albeit 2 years old. I will refer back here when next I create a service and and want the recovery attributes set programmatically during installation. Regards
Hi,
I have tried many solution as below
1. Wrote another thread to take care of restarting service but not feasible
2. Write another console application to restart the service but scheduling it is another headache
3. The most feasible solution is I wrote a batch file and scheduled it for every 4 hours to run and it works. Its a very easy and acceptable solution from time or efforts perpective.
Thanks,
Shrikant
 
Share this answer
 
Comments
MatthysDT 21-May-14 3:06am    
Hi Shrikant, the accepted solution for this post was Application.Restart() and it works like a charm!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900