WMI(Windows管理规范)的一个实现

http://tech.ddvip.com   2006年07月20日    社区交流

本文详细介绍WMI(Windows管理规范)的一个实现

  终止一个特定的进程与启动或停止一个服务类似。首先还是要取得选中进程对应的 ManagementObject 对象,然后通过调用InvokeMethod(observer, "Terminate", null) 来杀死一个进程。

//设置一个异步回调的句柄
ManagementOperationObserver observer = new ManagementOperationObserver();
completionHandler.MyHandler completionHandlerObj = new completionHandler.MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);
//获取进程的ManagementObject
queryCollection = getProcessCollection("Select * from Win32_Process Where ProcessID =
        ''" + ProcessID + "''");
//更新状态条
updateStatus("Invoking terminate process");
foreach ( ManagementObject mo in queryCollection)
{
  //启动或者停止服务(译注:作者真懒?)
  mo.InvokeMethod(observer, "Terminate", null);
}
//等待,直到invoke调用完成或者超时5秒后
int intCount = 0;
while (!completionHandlerObj.IsComplete)
{
  if (intCount == 10)
  {
    MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
    break;
  }
  //等待0.5秒
  System.Threading.Thread.Sleep(500);
  
  //增加计数器
  intCount++;
}
if (intCount != 10)
{
  //InvokeMethod尚未超时
  if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
  {
    lvItem = ProcessItem;
    lvItem.Remove();
  }
  else
  {
    MessageBox.Show("Error terminating process.",
      "Terminate Process");
  }
}

  创建进程

作者:Abbey    责编:豆豆技术应用

正在加载评论...