Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 10k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Diagnostics;
  8. namespace ch4_1
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.MainMenu mainMenu1;
  16. private System.Windows.Forms.Panel panel1;
  17. private System.Windows.Forms.ListView listView1;
  18. private System.Windows.Forms.Button button1;
  19. private System.Windows.Forms.Button button2;
  20. private System.Windows.Forms.Button button3;
  21. private System.Windows.Forms.GroupBox groupBox1;
  22. private System.Windows.Forms.ColumnHeader columnHeader1;
  23. private System.Windows.Forms.ColumnHeader columnHeader2;
  24. private System.Windows.Forms.ColumnHeader columnHeader3;
  25. private System.Windows.Forms.ColumnHeader columnHeader4;
  26. private System.Windows.Forms.ColumnHeader columnHeader5;
  27. private System.Windows.Forms.ColumnHeader columnHeader6;
  28. private System.Windows.Forms.ColumnHeader columnHeader7;
  29. /// <summary>
  30. /// 必需的设计器变量。
  31. /// </summary>
  32. private System.ComponentModel.Container components = null;
  33. public Form1()
  34. {
  35. //
  36. // Windows 窗体设计器支持所必需的
  37. //
  38. InitializeComponent();
  39. UpdateProcess();
  40. //
  41. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  42. //
  43. }
  44. /// <summary>
  45. /// 清理所有正在使用的资源。
  46. /// </summary>
  47. protected override void Dispose( bool disposing )
  48. {
  49. if( disposing )
  50. {
  51. if (components != null) 
  52. {
  53. components.Dispose();
  54. }
  55. }
  56. base.Dispose( disposing );
  57. }
  58. private void UpdateProcess()
  59. {
  60. //代表进程名
  61. string processName;
  62. //代表进程号
  63. int processID;
  64. //线程数
  65. int threadNumber;
  66. //cpu时间
  67. TimeSpan cpuTime;
  68. //占用的物理内存
  69. int memory;
  70. //优先级
  71. int priorityNumber;
  72. //启动时间
  73. DateTime startTime;
  74. //获取当前系统中的所有进程
  75. Process[] processes= Process.GetProcesses();
  76. //清除列表视图中原有的内容
  77. if (this.listView1!=null)
  78. {
  79. this.listView1.Items.Clear();
  80. }
  81. //在列表中加入每个进程项目的详细信息
  82. for(int i = 0; i < processes.Length; i++)
  83. {
  84. processName=processes[i].ProcessName;
  85. processID=processes[i].Id;
  86. threadNumber=processes[i].Threads.Count;
  87. cpuTime=processes[i].TotalProcessorTime;
  88. memory=processes[i].WorkingSet;
  89.   priorityNumber=processes[i].BasePriority;
  90. startTime=processes[i].StartTime;
  91. string prior="";
  92.             //判断优先级的值
  93. if (priorityNumber==13){prior="高";};
  94. if (priorityNumber==4){prior="低";};
  95. if (priorityNumber==8){prior="标准";};
  96. if (priorityNumber==24){prior="实时";};
  97.             //进程信息
  98. string[] subItems={
  99. processName,
  100. processID.ToString(),
  101. threadNumber.ToString(),
  102. String.Format("{0:00}:{1:00}:{2:00}",
  103. cpuTime.Hours, cpuTime.Minutes, cpuTime.Seconds),
  104. String.Format("{0:#,#,#}K", memory / 1024),
  105. prior,
  106. startTime.ToLongTimeString()
  107. };
  108. //构造一项并插入
  109. ListViewItem item=new ListViewItem(subItems);
  110. this.listView1.Items.Insert(i,item);
  111.     }
  112.     }
  113. #region Windows Form Designer generated code
  114. /// <summary>
  115. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  116. /// 此方法的内容。
  117. /// </summary>
  118. private void InitializeComponent()
  119. {
  120. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  121. this.panel1 = new System.Windows.Forms.Panel();
  122. this.listView1 = new System.Windows.Forms.ListView();
  123. this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
  124. this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
  125. this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
  126. this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
  127. this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
  128. this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
  129. this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
  130. this.button1 = new System.Windows.Forms.Button();
  131. this.button2 = new System.Windows.Forms.Button();
  132. this.button3 = new System.Windows.Forms.Button();
  133. this.groupBox1 = new System.Windows.Forms.GroupBox();
  134. this.panel1.SuspendLayout();
  135. this.groupBox1.SuspendLayout();
  136. this.SuspendLayout();
  137. // 
  138. // panel1
  139. // 
  140. this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
  141.  this.listView1});
  142. this.panel1.Location = new System.Drawing.Point(16, 16);
  143. this.panel1.Name = "panel1";
  144. this.panel1.Size = new System.Drawing.Size(328, 240);
  145. this.panel1.TabIndex = 0;
  146. // 
  147. // listView1
  148. // 
  149. this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  150. this.columnHeader1,
  151. this.columnHeader2,
  152. this.columnHeader3,
  153. this.columnHeader4,
  154. this.columnHeader5,
  155. this.columnHeader6,
  156. this.columnHeader7});
  157. this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
  158. this.listView1.FullRowSelect = true;
  159. this.listView1.Name = "listView1";
  160. this.listView1.Size = new System.Drawing.Size(328, 240);
  161. this.listView1.TabIndex = 0;
  162. this.listView1.View = System.Windows.Forms.View.Details;
  163. // 
  164. // columnHeader1
  165. // 
  166. this.columnHeader1.Text = "进程名";
  167. // 
  168. // columnHeader2
  169. // 
  170. this.columnHeader2.Text = "PID";
  171. this.columnHeader2.Width = 42;
  172. // 
  173. // columnHeader3
  174. // 
  175. this.columnHeader3.Text = "线程数";
  176. // 
  177. // columnHeader4
  178. // 
  179. this.columnHeader4.Text = "cpu占用时间";
  180. // 
  181. // columnHeader5
  182. // 
  183. this.columnHeader5.Text = "占用内存";
  184. // 
  185. // columnHeader6
  186. // 
  187. this.columnHeader6.Text = "优先级";
  188. // 
  189. // columnHeader7
  190. // 
  191. this.columnHeader7.Text = "启动时间";
  192. // 
  193. // button1
  194. // 
  195. this.button1.Location = new System.Drawing.Point(32, 24);
  196. this.button1.Name = "button1";
  197. this.button1.TabIndex = 1;
  198. this.button1.Text = "新建进程";
  199. this.button1.Click += new System.EventHandler(this.button1_Click);
  200. // 
  201. // button2
  202. // 
  203. this.button2.Location = new System.Drawing.Point(128, 24);
  204. this.button2.Name = "button2";
  205. this.button2.TabIndex = 2;
  206. this.button2.Text = "结束进程";
  207. this.button2.Click += new System.EventHandler(this.button2_Click);
  208. // 
  209. // button3
  210. // 
  211. this.button3.Location = new System.Drawing.Point(224, 24);
  212. this.button3.Name = "button3";
  213. this.button3.TabIndex = 3;
  214. this.button3.Text = "刷新列表";
  215. this.button3.Click += new System.EventHandler(this.button3_Click);
  216. // 
  217. // groupBox1
  218. // 
  219. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  220. this.button2,
  221. this.button3,
  222. this.button1});
  223. this.groupBox1.Location = new System.Drawing.Point(16, 272);
  224. this.groupBox1.Name = "groupBox1";
  225. this.groupBox1.Size = new System.Drawing.Size(328, 64);
  226. this.groupBox1.TabIndex = 4;
  227. this.groupBox1.TabStop = false;
  228. this.groupBox1.Text = "进程管理";
  229. // 
  230. // Form1
  231. // 
  232. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  233. this.ClientSize = new System.Drawing.Size(376, 349);
  234. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  235.   this.groupBox1,
  236.   this.panel1});
  237. this.Menu = this.mainMenu1;
  238. this.Name = "Form1";
  239. this.Text = "系统进程管理";
  240. this.panel1.ResumeLayout(false);
  241. this.groupBox1.ResumeLayout(false);
  242. this.ResumeLayout(false);
  243. }
  244. #endregion
  245. /// <summary>
  246. /// 应用程序的主入口点。
  247. /// </summary>
  248. [STAThread]
  249. static void Main() 
  250. {
  251. Application.Run(new Form1());
  252. }
  253. private void button1_Click(object sender, System.EventArgs e)
  254. {
  255. //构造打开文件对话框
  256. OpenFileDialog ofDlg = new OpenFileDialog();
  257. //选择可执行文件
  258. ofDlg.Filter = "可执行文件 (*.exe)|*.exe|所有文件(*.*)|*.*";
  259. //显示对话框
  260. if(ofDlg.ShowDialog() == DialogResult.OK)
  261. {
  262. try
  263. {
  264. //启动选择的程序
  265. Process newProcess = Process.Start(ofDlg.FileName);
  266. newProcess.EnableRaisingEvents=true;
  267. //为它添加退出进程的事件代理
  268. newProcess.Exited += new EventHandler(OnProcessExited);
  269. //更新列表
  270. UpdateProcess();
  271. }
  272. //参数错误异常
  273. catch(ArgumentException aError)
  274. {
  275. MessageBox.Show("出现错误:"+aError.Message, "警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  276. }
  277. }
  278. }
  279. private void OnProcessExited(object sender, EventArgs e)
  280. {
  281. //刷新ListView控件
  282. UpdateProcess();
  283. }
  284. private void button3_Click(object sender, System.EventArgs e)
  285. {
  286.     
  287. //刷新ListView控件
  288. UpdateProcess();
  289. }
  290. private void button2_Click(object sender, System.EventArgs e)
  291. {
  292. if(this.listView1.SelectedItems.Count>0)
  293. {
  294. //获取选中项目中的ID值
  295. int processID = Int32.Parse(listView1.SelectedItems[0].SubItems[1].Text);
  296. //根据ID获取进程
  297. Process selectedProcess = Process.GetProcessById(processID);
  298.                 selectedProcess.EnableRaisingEvents=true;
  299. //为它添加退出进程的事件代理
  300. selectedProcess.Exited += new EventHandler(OnProcessExited);
  301. //如果进程还没有退出
  302. if(!selectedProcess.HasExited)
  303. {
  304. //如果关闭主窗口失败
  305. if(!selectedProcess.CloseMainWindow())
  306. {
  307. //提示是否要杀死进程
  308. if(MessageBox.Show("不能关闭应用程序窗口,需要继续终止进程吗?",
  309. "提示",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  310. {
  311. try
  312. {
  313. //杀死进程
  314. selectedProcess.Kill();
  315. //更新列表
  316. UpdateProcess();
  317. }
  318. catch(Exception excep)
  319. {
  320. MessageBox.Show(excep.Message, "不能结束进程", MessageBoxButtons.OK,MessageBoxIcon.Error);
  321. }
  322. }
  323. }
  324. }
  325. else
  326. {
  327. MessageBox.Show("选中的进程已经退出!", "提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
  328. UpdateProcess();
  329. }
  330. }
  331. else
  332. {
  333. MessageBox.Show("请先选中一个当前正在运行的进程!", "提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
  334. }
  335. }
  336. }
  337. }