基本信息
源码名称:c# 监测进程cpu 占用率 过高则结束进程源码下载
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2014-01-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

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[i].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);
        }
    }
}