title: Wait for a service to completely Stop / Start before continuing
body: <p>Ok so... Simple enough. I've actually already gotten this to work for the most part. There are times, however, when the service hasn't actually stopped though the program seems to disagree. The program simply stops a service, copies files from one place to another, then starts a service. The service has a lock on the files being copied so it has to stop. </p>
  
  <pre><code>internal void StopTheService()
      {
          Logger.Log("Stopping the service.");
          lock (myLock) {
              var sc = new ServiceController() { ServiceName = nameOfService, MachineName = nameOfServerWithService };
              sc.Stop();
              sc.WaitForStatus(ServiceControllerStatus.Stopped);
          }
          Logger.Log("Service stopped."); 
      }
  
  internal void StartTheService()
      {
          Logger.Log("Starting the service.");
          lock (myLock) {
              var sc = new ServiceController() { ServiceName = nameOfService, MachineName = nameOfServerWithService };
              sc.Start();
              sc.WaitForStatus(ServiceControllerStatus.Running);
          }
          Logger.Log("Service Started.");
      }
  </code></pre>
  
  <p>This code works for the most part but issue is that when it thinks the service has stopped and moves on to the next step, the copy, it fails. I can wait for it to stop / start in a while loop like:</p>
  
  <pre><code>while(count&lt;5){
      sc.WaitForStatus(ServiceControllerStatus.Stopped);
      count = count+1;
  }
  </code></pre>
  
  <p>But I think that might fail as well. Everything I've found doesn't on the net doesn't exactly answer my question though my query may be incorrect. The application running this code is a multithreaded environment (hence the lock). If I rerun the application after failing, it works. The error it returns is your typical "file open in another process" error that you get when you try to open another file. Please note that this is a remote environment which has the services and files I'm accessing and yes I do have admin rights so UAC is not the issue.</p>
  
  <p>So the question: How can I be certain that the service has stopped or started completly before continuing? I'm open to almost any solution.</p>
  
  <p><strong>In Response to duplicate Question:</strong></p>
  
  <p>The thing that sets this apart is that it's a remote system that I am accessing in order to perform this work. The question noted as the duplicated question doesn't specify or show a remote scenario. I will test it however and should it succeed I will post it as an answer. However until then it is not a duplicate.</p>