博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 获取进程或线程的相关信息
阅读量:5157 次
发布时间:2019-06-13

本文共 7668 字,大约阅读时间需要 25 分钟。

信息来自: 

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace ConsoleApp{    class ProcessDo    {        ///         /// 获取进程相关信息        ///         public static void GetProcessInfomation()        {            Process pro = Process.GetProcessesByName("QQ")[0];            Console.WriteLine("进程名称:" + pro.ProcessName);            Console.WriteLine("进程ID:" + pro.Id.ToString());            Console.WriteLine("启动时间:" + pro.StartTime.ToLongDateString() + pro.StartTime.ToLongTimeString());            Console.WriteLine("是否响应:" + pro.Responding.ToString());            Console.WriteLine("关联进程句柄:" + pro.Handle.ToString());            Console.WriteLine("进程打开的句柄数:" + pro.HandleCount.ToString());            Console.WriteLine("主窗口句柄:" + pro.MainWindowHandle.ToString());            Console.WriteLine("主窗口标题:" + pro.MainWindowTitle);            Console.WriteLine("模块数量:" + pro.Modules.Count.ToString());            Console.WriteLine("基本优先级:" + pro.BasePriority.ToString());            Console.WriteLine("提升优先级:" + pro.PriorityBoostEnabled.ToString());            Console.WriteLine("处理器:" + pro.ProcessorAffinity.ToInt32().ToString());            Console.WriteLine("最小工作集:" + pro.MinWorkingSet.ToInt32().ToString());            Console.WriteLine("最大工作集:" + pro.MaxWorkingSet.ToInt32().ToString());            Console.WriteLine("工作集:" + pro.WorkingSet.ToString());            Console.WriteLine("峰值工作集:" + pro.PeakWorkingSet.ToString());            Console.WriteLine("专用内存大小:" + pro.PrivateMemorySize.ToString());            Console.WriteLine("未分页内存大小:" + pro.NonpagedSystemMemorySize.ToString());            Console.WriteLine("分页内存大小:" + pro.PagedMemorySize.ToString());            Console.WriteLine("峰值分页内存大小:" + pro.PeakPagedMemorySize.ToString());            Console.WriteLine("虚拟内存大小:" + pro.VirtualMemorySize.ToString());            Console.WriteLine("峰值虚拟内存大小:" + pro.PeakVirtualMemorySize.ToString());            Console.WriteLine("占用时间:" + pro.TotalProcessorTime.ToString());            Console.WriteLine("特权占用时间:" + pro.PrivilegedProcessorTime.ToString());            Console.WriteLine("用户占用时间:" + pro.UserProcessorTime.ToString());        }        ///         /// 进程的线程详细信息        ///         public static void GetProcessThreadInfomation()        {            Process pro = Process.GetProcessesByName("QQ")[0];            Console.WriteLine("QQ程序进程的线程详细信息如下:");            int length = pro.Threads.Count;            for (int i = 0; i < length; i++)            {                var thread = pro.Threads[i];                Console.WriteLine("标识符:" + thread.Id.ToString());                Console.WriteLine("基本优先级:" + thread.BasePriority.ToString());                Console.WriteLine("当前优先级:" + thread.CurrentPriority.ToString());                Console.WriteLine("内存地址:" + thread.StartAddress.ToInt32());                Console.WriteLine("启动时间:" + thread.StartTime.ToString());                Console.WriteLine("使用时间:" + thread.UserProcessorTime.ToString());                Console.Write("当前状态:");                switch (thread.ThreadState)                {                    case ThreadState.Initialized:                        Console.WriteLine("线程已经初始化但尚未启动");                        break;                    case ThreadState.Ready:                        Console.WriteLine("线程准备在下一个可用的处理器上运行");                        break;                    case ThreadState.Running:                        Console.WriteLine("当前正在使用处理器");                        break;                    case ThreadState.Standby:                        Console.WriteLine("线程将要使用处理器");                        break;                    case ThreadState.Terminated:                        Console.WriteLine("线程已完成执行并退出");                         break;                    case ThreadState.Transition:                        Console.WriteLine("线程在可以执行钱等待处理器之外的资源");                        break;                    case ThreadState.Unknown:                        Console.WriteLine("状态未知");                        break;                    case ThreadState.Wait:                        Console.WriteLine("正在等待外围操作完成或者资源释放");                        break;                    default:                        break;                }                if (thread.ThreadState == ThreadState.Wait)                {                    Console.Write("等待原因:");                    switch (thread.WaitReason)                    {                        case ThreadWaitReason.EventPairHigh:                            Console.WriteLine("线程正在等待事件对高");                            break;                        case ThreadWaitReason.EventPairLow:                            Console.WriteLine("线程正在等待事件对低");                            break;                        case ThreadWaitReason.ExecutionDelay:                            Console.WriteLine("线程执行延迟");                            break;                        case ThreadWaitReason.Executive:                            Console.WriteLine("线程正在等待计划程序");                            break;                        case ThreadWaitReason.FreePage:                            Console.WriteLine("线程正在等待可用的虚拟内存页");                            break;                        case ThreadWaitReason.LpcReceive:                            Console.WriteLine("线程正在等待本地过程调用到达");                            break;                        case ThreadWaitReason.LpcReply:                            Console.WriteLine("线程正在等待对本地过程调用的回复到达");                            break;                        case ThreadWaitReason.PageIn:                            Console.WriteLine("线程正在等待虚拟内存页到达内存");                            break;                        case ThreadWaitReason.PageOut:                            Console.WriteLine("线程正在等待虚拟内存页写入磁盘");                            break;                        case ThreadWaitReason.Suspended:                            Console.WriteLine("线程执行暂停");                            break;                        case ThreadWaitReason.SystemAllocation:                            Console.WriteLine("线程正在等待系统分配");                            break;                        case ThreadWaitReason.Unknown:                            Console.WriteLine("线程因位置原因而等待");                            break;                        case ThreadWaitReason.UserRequest:                            Console.WriteLine("线程正在等待用户请求");                            break;                        case ThreadWaitReason.VirtualMemory:                            Console.WriteLine("线程正在等待系统分配虚拟内存");                            break;                        default:                            break;                    }                }                Console.WriteLine();             }        }        ///         /// 限制应用程序运行时间        /// 即关联程序超出设置的运行将自动关闭        ///         public static void GetKillAppForWaitTime()        {            Process proc = new Process();            proc.StartInfo = new ProcessStartInfo(@"C:\Documents and Settings\zkk\桌面\mysql_20120925.sql");            //启动应用程序            proc.Start();            //等待程序师徒完成载入            proc.WaitForInputIdle();            //等待程序结束执行            proc.WaitForExit(3000);            //如果程序在时间期限之前关闭,HasExited将会是true            if (proc.HasExited == false)            {                //测试程序是否已经停止回应                if (proc.Responding)                {                    //程序有回应,关闭主视图                    proc.CloseMainWindow();                }                else                {                    //如果程序没有回应,将强制关闭                    proc.Kill();                }            }        }    }}

 

转载于:https://www.cnblogs.com/vincentDr/p/3465058.html

你可能感兴趣的文章
ffplay.exe操作方式
查看>>
TestNG
查看>>
[算法]复杂链表的复制
查看>>
【LeetCode】437. Path Sum III
查看>>
求最近点对算法分析 closest pair algorithm
查看>>
移动端点击输入框,弹出键盘,底部被顶起问题(vue)
查看>>
[数据]全拼-简体字对照表
查看>>
CentOS+Subversion
查看>>
php算法和数据结构
查看>>
[翻译] CHAnimation
查看>>
智力问答 48倒计时
查看>>
返回一个整数数组中最大子数组的和---环形数组
查看>>
前端之Css
查看>>
springboot 修改页面不重启
查看>>
交互式编程之Golang基本配置(Jupyter-notebooks Golang)
查看>>
celery
查看>>
minio 搭建blob
查看>>
一个Demo带你彻底掌握View的滑动冲突
查看>>
Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
查看>>
【学习记录】第一章 数据库设计-《SQL Server数据库设计和开发基础篇视频课程》...
查看>>