Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot restart a Service
    text
    copied!<p>I have this code to restart a service, but this is not working.</p> <p>I can start and stop individually but not restart which involves me to first stop and start the service.</p> <pre><code>try { //service.Stop(); //service.Start(); 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 { // ... } </code></pre> <p>It is simply going in the catch section.</p> <p>I don't know where i am going wrong.</p> <p>Any suggestions.??</p> <p>UPDATE:</p> <p>So I took the idea from the correct answer below:</p> <p>This is what need to be done></p> <pre><code>public static void RestartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); if (!(service.Status.Equals(ServiceControllerStatus.Stopped) || service.Status.Equals(ServiceControllerStatus.StopPending))) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } // count the rest of the timeout int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1)); if (!(service.Status.Equals(ServiceControllerStatus.Running) || service.Status.Equals(ServiceControllerStatus.StartPending))) { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload