title: Wait for service.InvokeMethod to finish - WMI, C#
body: <p>i'm stoppinga service on remote machine with WMI:  </p>
  
  <pre><code>    protected override void Execute()
      {
          ConnectionOptions connectoptions = new ConnectionOptions();
          connectoptions.Username = RemoteMachineUsername;
          connectoptions.Password = RemoteMachinePassword;
  
          ManagementScope scope = new ManagementScope(@"\\" + RemoteMachineName + @"\root\cimv2");
          scope.Options = connectoptions;
          SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + ServiceName + "'");
          using (ManagementObjectSearcher searcher = new
                      ManagementObjectSearcher(scope, query))
          {
              ManagementObjectCollection collection = searcher.Get();
  
                  foreach (ManagementObject service in collection)
                  {
                      if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
                      {
                          //Stop the service
                          service.InvokeMethod("StopService", null);//HOW TO WAIT FOR THIS TO FINISH?
                      }
                  }
          }
      }
  </code></pre>
  
  <p>now, this method finished long before the service is stopped. my question is, how can i wait for the service to be stopped and how can i know if it succeeded. in other words, i want to do this in a sync way.</p>
  
  <p>thanks!</p>