title: How to restart Windows service by itself C#
body: <p>I have created a service, where I am sending commands to the service via MSMQ. </p>
  
  <p>Now I need to restart the service but when it reaches to the stop command it just throws error or it is not able to read the next line as the service performing this task stops.</p>
  
  <p>The code I am using is :</p>
  
  <pre><code>ServiceController service = new ServiceController(serviceName);
              try
              {
                  int millisec1 = Environment.TickCount;
                  TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                  ShutdownService(serviceName, timeoutMilliseconds);
  
                  // count the rest of the timeout
                  int millisec2 = Environment.TickCount;
                  timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
  
                  service.Start();
                  service.WaitForStatus(ServiceControllerStatus.Running, timeout);
              }
              catch (Exception ex)
              {
                  s_log.Error(ex);
              }
  </code></pre>
  
  <p>How can I restart service by itself as logically when the service will stop, it will not execute next line of code.</p>