实现效果:
知识运用:
Process类的Kill方法
public void Kill () //用来立刻停止关联的进程
PriorityClass属性 //获取或设置关联进程的总体优先级类别
public ProcessPriorityClass PriorityClass { get; set; }
实现代码:
//定义添加方法 private void getProcessInfo() { listView1.Items.Clear(); Process[] myProcess = Process.GetProcesses(); string[] Minfo = new string[6]; foreach (Process pro in myProcess) { Minfo[0] = pro.ProcessName; Minfo[1] = pro.Id.ToString(); Minfo[2] = pro.Threads.Count.ToString(); Minfo[3] = pro.BasePriority.ToString(); Minfo[4] = (pro.WorkingSet / 1024) + "K"; Minfo[5] = (pro.VirtualMemorySize / 1024) + "k"; ListViewItem item = new ListViewItem(Minfo); listView1.Items.Add(item); } this.toolStripStatusLabel1.Text ="进程总数: "+ Process.GetProcesses().Count(); } //设置进程优先级 private void setBasePriority(int n) { Process[] prs = Process.GetProcessesByName(listView1.SelectedItems[0].Text); switch (n) { case 1:prs[0].PriorityClass=ProcessPriorityClass.Idle; break; //低优先级 case 2:prs[0].PriorityClass=ProcessPriorityClass.Normal; break; //标准 case 3:prs[0].PriorityClass=ProcessPriorityClass.High; break; //高 case 4:prs[0].PriorityClass=ProcessPriorityClass.RealTime; break; //实时 case 5:prs[0].PriorityClass=ProcessPriorityClass.AboveNormal; break; //高于标准 case 6:prs[0].PriorityClass = ProcessPriorityClass.BelowNormal; break; //低于标准 } getProcessInfo(); } //初始化 private void Form1_Load(object sender, EventArgs e) { getProcessInfo(); } private void 结束进程ToolStripMenuItem_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { try { if (MessageBox.Show("警告:终止进程会导致不希望发生的结果,\r包括数据丢失和系统不稳定。在被终止前,\r进程将没有机会保存其状态和数据。确认\r要终止进程吗?", "任务管理器警告", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { Process[] ps = Process.GetProcessesByName(listView1.SelectedItems[0].Text); foreach(Process p in ps) p.Kill(); getProcessInfo(); } } catch (Exception) { string total = listView1.SelectedItems[0].Text; Process[] prs = Process.GetProcessesByName(total); Process myProcess = new Process(); myProcess.StartInfo.FileName="cmd.exe"; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardError = true; //执行关闭命令 myProcess.Start(); myProcess.StandardInput.WriteLine("TASKKILL /f /pid "+prs[0].Id); myProcess.StandardInput.WriteLine("exit"); getProcessInfo(); } } } private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e) { getProcessInfo(); } private void 实时ToolStripMenuItem_Click(object sender, EventArgs e) { setBasePriority(4); } private void 高ToolStripMenuItem_Click(object sender, EventArgs e) { setBasePriority(3); } private void 高于标准ToolStripMenuItem_Click(object sender, EventArgs e) { setBasePriority(5); } private void 标准ToolStripMenuItem_Click(object sender, EventArgs e) { setBasePriority(2); } private void 低于标准ToolStripMenuItem_Click(object sender, EventArgs e) { setBasePriority(6); } private void 低ToolStripMenuItem_Click(object sender, EventArgs e) { setBasePriority(1); } //实现对应选项是否可用 private void contextMenuStrip1_MouseEnter(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) 设置优先级ToolStripMenuItem.Enabled = false; else 设置优先级ToolStripMenuItem.Enabled = true; }