服务器之家

服务器之家 > 正文

C#获取进程或线程相关信息的方法

时间:2021-10-21 11:46     来源/作者:我心依旧

本文实例讲述了C#获取进程或线程相关信息的方法。分享给大家供大家参考。具体实现方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApp
{
 class ProcessDo
 {
  /// <summary>
  /// 获取进程相关信息
  /// </summary>
  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());
  }
  /// <summary>
  /// 进程的线程详细信息
  /// </summary>
  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();
    }
  }
  /// <summary>
  /// 限制应用程序运行时间
  /// 即关联程序超出设置的运行将自动关闭
  /// </summary>
  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();
    }
   }
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部