How can a windows service programmatically restart itself? How can a windows service programmatically restart itself? windows windows

How can a windows service programmatically restart itself?


Set the service to restart after failure (double click the service in the control panel and have a look around on those tabs - I forget the name of it). Then, anytime you want the service to restart, just call Environment.Exit(1) (or any non-zero return) and the OS will restart it for you.


Dim proc As New Process()Dim psi As New ProcessStartInfo()psi.CreateNoWindow = Truepsi.FileName = "cmd.exe"psi.Arguments = "/C net stop YOURSERVICENAMEHERE && net start YOURSERVICENAMEHERE"psi.LoadUserProfile = Falsepsi.UseShellExecute = Falsepsi.WindowStyle = ProcessWindowStyle.Hiddenproc.StartInfo = psiproc.Start()


You can't be sure that the user account that your service is running under even has permissions to stop and restart the service.