C#計算機進程管理工具,根據CPU結束相應進程
這個工具是當CPU達到我們設置的相應值時,會自動結束設置好的進程。
先來看看界面吧
源碼下載:
http://www.cckan.net/thread-1791-1-1.html
好了我們一下來看看代碼吧,具體的我就不多說了,代碼很簡單,很容易看明白,如果確實有不明白的地方
要吧直接回復提問,
上代碼
/// <summary> /// 更新網站:<a href=\"http://www.cckan.net/thread-1791-1-1.html\" target=\"_blank\">http://www.cckan.net/thread-1791-1-1.html</a> /// </summary> using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Threading; namespace startName { public partial class Form1 : Form { string cpu = string.Empty; string info = string.Empty; double cpuMax = 0; string ProcessName = ""; PerformanceCounter MainPC = new PerformanceCounter();//性能計數器 Process[] p = null; PerformanceCounter ChPC = new PerformanceCounter(); public Form1() { InitializeComponent(); } //開始執行 private void button1_Click(object sender, EventArgs e) { try { panel1.Enabled = false; timer1.Enabled = true; try { //設置一下執行的參數 timer1.Interval = 1000 * Convert.ToInt32(textBox4.Text.Trim()); } catch (Exception) { timer1.Interval = 1000 * 5; } MainPC.CategoryName = "Processor";//指定獲取計算機進程信息如果傳Processor參數代表查詢計算機CPU MainPC.CounterName = "% Processor Time";//占有率 //如果pp.CategoryName="Processor",那么你這里賦值這個參數 pp.InstanceName = "_Total"代表查詢本計算機的總CPU。 MainPC.InstanceName = "_Total";//指定進程 MainPC.MachineName = "."; cpuMax = Convert.ToDouble(textBox1.Text.Trim()); ProcessName = textBox2.Text.Trim(); p = System.Diagnostics.Process.GetProcessesByName(ProcessName); textBox3.Text += textBox2.Text + "進程數:" + p.Length.ToString() + "\r\n"; } catch (Exception) { timer1.Enabled = false; panel1.Enabled = true; } } private void timer1_Tick(object sender, EventArgs e) { try { double dcpu = Math.Round(MainPC.NextValue(), 2); label3.Text = "當前計算機CPU使用率:" + dcpu; textBox3.ScrollToCaret(); if (dcpu > cpuMax) { int le = 0; int num = 0; le = p.Length; for (int i = 0; i < le; i++) { textBox3.ScrollToCaret(); p.Kill(); num++; } textBox3.Text += DateTime.Now.ToString() + " Cpu:" + dcpu.ToString() + " 成功清理:" + num.ToString() + "個進程\r\n"; } } catch (Exception ex) { textBox3.Text += "出現問題:" + ex.Message.Trim() + "\r\n"; } } private void Form1_Load(object sender, EventArgs e) { } private void timer2_Tick(object sender, EventArgs e) { p = System.Diagnostics.Process.GetProcessesByName(ProcessName); } } }
其實這咱種工具還是很好用的,放在服務器上,如果電腦Cpu過高的話是可以直接維護的,
不用人工的再去處理了
不知道大家有沒有這種情況,這是我在我服務器是用的一個小工具,給大家分享一下。希望大家喜歡
代碼里面主要是運用了一些進程相關的操作方法,還有一些關於Cpu統計的一些方式。
好了就這樣吧,具體的大家看代碼吧,有什么建議請您回復我吧。期待大家的建議