最近看到一篇文章,就是如何对系统某个进程进行监控,并且当这个进程触发某些事件的时候,能进行相应。而且发现有人问这方面的问题,我就大致在其原有的基础进行如下的修改。
首先说明的一点,方法是基于WMI的。以下是我扩展类的代码说明:
//------------------------ProcessInfo Class------------------------------------
//-----------------------------------------------------------------------------
//---File:clsProcessInfo.cs
//---Description:This class demonstrates the use of WMI.
// It provides a static method to query the list of running processes.
// And it provides two delegated events binding specific application.
//---Author:Knight
//---Date:Mar.21, 2006
//-----------------------------------------------------------------------------
//----------------------{ ProcessInfo Class }----------------------------------
using System;
using System.Data;
using System.Management;
using System.Diagnostics;
namespace WinProcess
{
/// <summary>
/// ProcessInfo class.
/// </summary>
public class ProcessInfo
{
// defenition of the delegates
public delegate void StartedEventHandler(object sender, EventArgs e);
public delegate void TerminatedEventHandler(object sender, EventArgs e);
// events to subscribe
public StartedEventHandler Started = null;
public TerminatedEventHandler Terminated = null;
// WMI event watcher
private ManagementEventWatcher watcher;
/// <summary>
/// Construction that binds specific application with event declared
/// </summary>
/// <param name="LocalServerName"></param>
/// <param name="appName"></param>
public ProcessInfo( string appName)
{
// querry every 2 seconds
string pol = "2";
string queryString =
"S