如何对系统中的某个进程进行监控

本文介绍如何利用WMI(Windows Management Instrumentation)来监控系统中的特定进程。通过创建一个名为ProcessInfo的类,实现了对进程启动和终止事件的监听。该类包含静态方法用于获取本地或远程服务器上的运行进程列表,并提供了事件委托以响应进程的开始和结束。示例代码展示了如何监控记事本程序(notepad.exe)的启动和结束事件。
摘要由CSDN通过智能技术生成

最近看到一篇文章,就是如何对系统某个进程进行监控,并且当这个进程触发某些事件的时候,能进行相应。而且发现有人问这方面的问题,我就大致在其原有的基础进行如下的修改。

 

首先说明的一点,方法是基于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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值