当前位置:编程学习 > C#/ASP.NET >>

进程监视类ProceWatcher

/*========================================================
 * Author:myhuang
 * Date:2007-02-26
 * Note:获取进程主要属性值
 ========================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Management;

namespace ProceWatcher
{
    /// <summary>
    /// 进程类
    /// </summary>
    public class MyProcess
    {
        /// <summary>
        /// 进程ID
        /// </summary>
        public int ProceID;
        /// <summary>
        /// 进程名称
        /// </summary>
        public string ProceName;
        /// <summary>
        /// 执行路径
        /// </summary>
        public string ExecPath;
        /// <summary>
        /// 创建者
        /// </summary>
        public string Creator;
        /// <summary>
        /// 创建时间
        /// </summary>
        public string CreateTime;
        /// <summary>
        /// 线程数
        /// </summary>
        public int ThreadsCount;
        /// <summary>
        /// CPU占用率
        /// </summary>
        public double CpuPercent;
        /// <summary>
        /// 上次CPU时间
        /// </summary>
        public long OldCpuTime;
        /// <summary>
        /// 物理内存(KB)
        /// </summary>
        public long MemoryKB;
        /// <summary>
        /// 虚拟内存(KB)
        /// </summary>
        public long VirtualKB;
        /// <summary>
        /// 父进程名
        /// </summary>
        public string ParentProce;
        /// <summary>
        /// 公司信息
        /// </summary>
        public string CompanyName;
        /// <summary>
        /// 是否是目标进程
        /// </summary>
        public bool Target;
        /// <summary>
        /// 是否活动
        /// </summary>
        public bool Active;
    }

    #region 委    托
    public delegate void DlgProceNew(MyProcess pProce);
    public delegate void DlgProceClosed(MyProcess pProce);
    public delegate void DlgProceRefresh(MyProcess pProce,double pCpuPercent);
    public delegate void DlgDetailList(MyProcess pProce);
    #endregion

    /// <summary>
    /// 该类提供获取进程信息的功能
    /// </summary>
    public class ProcessInfo
    {
        #region 私有成员
        /// <summary>
        /// 字典类,用于保存各进程信息
        /// </summary>
        private Dictionary<int, MyProcess> mDict;
        /// <summary>
        /// 当前总CPU时间
        /// </summary>
        private double mCurrentTotalCpuTime;
        /// <summary>
        /// CPU空闲比率
        /// </summary>
        private double mIdleCpuPercent;
        /// <summary>
        /// 当前进程
        /// </summary>
        private Process[] mCurrentAll;
        /// <summary>
        /// 性能计数器,用于获取CPU空闲百分比
        /// </summary>
        private static PerformanceCounter mIdle = new PerformanceCounter("Process", "% Processor Time", "Idle");
        /// <summary>
        /// 性能计数器,用于获取CPU总利用率
        /// </summary>
        private static PerformanceCounter mTotal = new PerformanceCounter("Process", "% Processor Time", "_Total");
        private object mLock = new object();
        #endregion

        #region 事    件
        /// <summary>
        /// 当出现新进程时触发该事件
        /// </summary>
        public event DlgProceNew HandleProceNew;
        /// <summary>
        /// 当进程关闭

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,