How can I restart the Windows service programmatically in .NET.

How can I restart the windows service programmatically in .NET?
In addition, I need to perform the operation when the restart of the service is complete.

+44
c # windows-services
Sep 21 '09 at 13:27
source share
8 answers

Take a look at the ServiceController class.

To perform the operation that should be performed when the service is restarted, I think you should do it in the Service yourself (if this is your own service).
If you do not have access to the source of the service, perhaps you can use the WaitForStatus method for the ServiceController .

+29
Sep 21 '09 at 13:29
source share

This article uses the ServiceController class for write methods to start, stop, and restart Windows services; it may be worth a look at.

Excerpt from the article (method "Restarting the service"):

 public static void RestartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); // count the rest of the timeout int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1)); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } 
+58
Sep 21 '09 at 13:32
source share

ServiceController Class Example

 private void RestartWindowsService(string serviceName) { ServiceController serviceController = new ServiceController(serviceName); try { if ((serviceController.Status.Equals(ServiceControllerStatus.Running)) || (serviceController.Status.Equals(ServiceControllerStatus.StartPending))) { serviceController.Stop(); } serviceController.WaitForStatus(ServiceControllerStatus.Stopped); serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running); } catch { ShowMsg(AppTexts.Information, AppTexts.SystematicError, MessageBox.Icon.WARNING); } } 
+18
Oct 02 '13 at 21:05
source share

You can also invoke the net command to do this. Example:

 System.Diagnostics.Process.Start("net", "stop IISAdmin"); System.Diagnostics.Process.Start("net", "start IISAdmin"); 
+13
Sep 21 '09 at 13:42
source share

What about

 var theController = new System.ServiceProcess.ServiceController("IISAdmin"); theController.Stop(); theController.Start(); 

Be sure to add System.ServiceProcess.dll to your project for this to work.

+4
Sep 21 '09 at 13:32
source share

See the article.

Here is a snippet from the article .

 //[QUICK CODE] FOR THE IMPATIENT using System; using System.Collections.Generic; using System.Text; // ADD "using System.ServiceProcess;" after you add the // Reference to the System.ServiceProcess in the solution Explorer using System.ServiceProcess; namespace Using_ServiceController{ class Program{ static void Main(string[] args){ ServiceController myService = new ServiceController(); myService.ServiceName = "ImapiService"; string svcStatus = myService.Status.ToString(); if (svcStatus == "Running"){ myService.Stop(); }else if(svcStatus == "Stopped"){ myService.Start(); }else{ myService.Stop(); } } } } 
+2
Sep 21 '09 at 13:32
source share

This answer is based on @Donut Answer (the most accurate answer to this question), but with some changes.

  • Eliminating the ServiceController class after each use, as it implements the IDisposable interface.
  • Reduce method parameters: there is no need to pass the serviceName parameter for each method, we can set it in the constructor, and every other method will use this service name.
    It is also more suitable for OOP.
  • Handle the catch exception so that this class can be used as a component.
  • Remove the timeoutMilliseconds parameter from each method.
  • Add two new methods, StartOrRestart and StopServiceIfRunning , which can be thought of as a wrapper for other base methods. The purpose of this method is only to avoid exceptions, as described in the comment.

Here is the class

 public class WindowsServiceController { private string serviceName; public WindowsServiceController(string serviceName) { this.serviceName = serviceName; } // this method will throw an exception if the serivce is NOT in Running status. public void RestartService() { using (ServiceController service = new ServiceController(serviceName)) { try { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } catch (Exception ex) { throw new Exception($"Can not restart the Windows Service {serviceName}", ex); } } } // this method will throw an exception if the serivce is NOT in Running status. public void StopService() { using (ServiceController service = new ServiceController(serviceName)) { try { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); } catch (Exception ex) { throw new Exception($"Can not Stop the Windows Service [{serviceName}]", ex); } } } // this method will throw an exception if the service is NOT in Stopped status. public void StartService() { using (ServiceController service = new ServiceController(serviceName)) { try { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } catch (Exception ex) { throw new Exception($"Can not Start the Windows Service [{serviceName}]", ex); } } } // if service running then restart the serivce, if the service is stopped then start it. // this method will not throw an exception. public void StartOrRestart() { if (IsRunningStatus) RestartService(); else if (IsStoppedStatus) StartService(); } // stop the service if it is running, if it is already stopped then do nothing. // this method will not throw an exception if the service is in Stopped status. public void StopServiceIfRunning() { using (ServiceController service = new ServiceController(serviceName)) { try { if (!IsRunningStatus) return; service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); } catch (Exception ex) { throw new Exception($"Can not Stop the Windows Service [{serviceName}]", ex); } } } public bool IsRunningStatus => Status == ServiceControllerStatus.Running; public bool IsStoppedStatus => Status == ServiceControllerStatus.Stopped; public ServiceControllerStatus Status { get { using (ServiceController service = new ServiceController(serviceName)) { return service.Status; } } } } 
+2
May 20 '17 at 9:01 a.m.
source share

Call Environment.Exit with an error code greater than 0, which seems appropriate, and then during installation we will configure the service to restart if an error occurs.

 Environment.Exit(1); 

I did the same in my service. It is working fine.

-2
Nov 27 '13 at 10:11
source share



All Articles