我正在使用 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?
}
}
}
}
现在,此方法在服务停止之前很久就完成了。我的问题是,我怎样才能等待服务停止,我怎么知道它是否成功。换句话说,我想以同步的方式做到这一点。
谢谢!