티스토리 뷰


         public Form1()
        {
            InitializeComponent();

            Start();
        }

        // PerforanceCounter 클래스 !
        public PerformanceCounter CPU = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        public bool bExit = false;
        // 점유율 표기
        public int  iCPU = 0;
        public Font F = new Font("굴림", 9);
        public Thread checkThread;

        public void Start()
        {
            CheckForIllegalCrossThreadCalls = false;

            // 지속적인 확인을 위해서 Thread 활용
            checkThread = new Thread(getCPU_Info);
            checkThread.Start();
        }

        public void getCPU_Info()
        {
            while (!bExit)
            {
                iCPU = (int)CPU.NextValue();
                this.Text = "CPU 사용 : " + iCPU.ToString() + "%";
                iCPU *= 3;
                panel1.Invalidate();
                Thread.Sleep(500);
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (iCPU <= 20)
                g.FillRectangle(Brushes.BlanchedAlmond, 0, 0, iCPU, panel1.Height);
            if (iCPU <= 40)
                g.FillRectangle(Brushes.Wheat, 0, 0, iCPU, panel1.Height);
            if (iCPU <= 60)
                g.FillRectangle(Brushes.NavajoWhite, 0, 0, iCPU, panel1.Height);
            if (iCPU <= 80)
                g.FillRectangle(Brushes.Orange, 0, 0, iCPU, panel1.Height);
            else
                g.FillRectangle(Brushes.DarkOrange, 0, 0, iCPU, panel1.Height);

            iCPU /= 3;
            g.DrawString(iCPU.ToString() + "%", F, Brushes.DarkRed, panel1.Width / 2 - 17, panel1.Height / 4);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            checkThread.Abort();
        }




performanceClass 를 활용해서 현재 CPU 점유율을 확인.


프로세스 별이 아닌 총 CPU 임을 감안해야 함.


활용 용도는 cpu 점유율이 특정 퍼센트를 넘으면 프로세스 킬을 해야 함


-> 점유율이 50 이상이 되었을 때, Process list 를 받아오고 불필요한 프로세스를 점검 후 kill
댓글
댓글쓰기 폼