i’m stoppinga service on remote machine with WMI:
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?
}
}
}
}
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.
thanks!
StopService returns a
uintstatus code you you can cast the result of yourInvokeMethodall to anuintand check its return value.The call already should be synchronous, but it could be timing out if the service doesn’t respond to the stop request immediately. If that is the case you could always loop on checking the service
Stateproperty.