MessageBox.Show("Failed to " + stringAction + " service " + ServiceName + ".",
"Start/Stop Service Failure");
}
//clean-up objects
ServiceName = "";
ServiceAction = "";
ServiceItem = null;
//Status
updateStatus("Ready");
this.Update();
}
//----------------------------------
// Completion Handler
//----------------------------------
using System;
using System.Management;
namespace completionHandler
{
/// <summary>
/// MyHandler class handle notification when InvokeMethod call is complete
/// </summary>
public class MyHandler
{
private bool isComplete = false;
private ManagementBaseObject returnObject;
/// <summary>
/// Trigger Done event when InvokeMethod is complete
/// </summary>
public void Done(object sender, ObjectReadyEventArgs e)
{
isComplete = true;
returnObject = e.NewObject;
}
/// <summary>
/// Get property IsComplete
/// </summary>
public bool IsComplete
{
get
{
return isComplete;
}
}
/// <summary>
/// Property allows accessing the result object in the main function
/// </summary>
public ManagementBaseObject ReturnObject
{
get
{
return returnObject;
}
}
}
}
ProcessesControl控件介绍:
该控件主要用来显示系统中正在运行的进程,如:用户进程.CPU利用率,
内存的使用状况.我们可以通过GetOwner(User,Domain)方法来得知进程
的所有者是谁.User和Domain是入口参数,可是问题是我们如何从InvokeMethod
中得到这入口参数呢?这里我们需要实现InvokeMethod.一下讨论两种情况
1.我们不需要异步操作,我们仅仅只需要一个string数组就可以完成
2.当我们需要异步操作的时候也只需要一个completionHandlerObj.ReturnObject
属性来收集对象.
代码如下:
//-------------------------------------------------
//Get process owner info without the observer object
//--------------------------------------------------
//Createan array containing all arguments for the method
string methodArgs = {"", ""};
//Get process owner info
mo.InvokeMethod("GetOwner", methodArgs);
//methodArgs[0] - contain process user
//methodArgs = contain process domain
//-----------------------------------------------
//Getprocess owner info with the observer object
//-----------------------------------------------
mo.InvokeMethod(observer,"GetOwner", null);
while (!completionHandlerObj.IsComplete)
{
System.Threading.Thread.Sleep(500);
}
if (completionHandlerObj.ReturnObject["returnValue"].ToString() == "0")
structProcess.stringUserName = completionHandlerObj.ReturnObject.Properties["User"].Value.ToString();
else
structProcess.stringUserName = "";
下面讨论如何终结进程:
终结一个指定的进程很类似与上面提到的启动或终止一项服务.
首先当然是用ManagementObject对象来指定你要的进程,然后调用
InvokeMethod(observer,"Terminate",null)来终止一个进程.
代码如下:
//Set up a handler for the as