첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ PerformanceCounter 클래스 : CPU 사용률 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Diagnostics;

using System.Drawing;

using System.Threading;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼 입니다.

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region CPU 이용률 설정하기 대리자 - SetCPUUsageRateDelegate(cpuUsageRate)

 

        /// <summary>

        /// CPU 이용률 설정하기 대리자

        /// </summary>

        /// <param name="cpuUsageRate">CPU 이용률</param>

        private delegate void SetCPUUsageRateDelegate(int cpuUsageRate);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 스레드

        /// </summary>

        private Thread thread;

 

        /// <summary>

        /// 성능 카운터

        /// </summary>

        private PerformanceCounter performanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

 

        /// <summary>

        /// CPU 이용률 대리자

        /// </summary>

        private SetCPUUsageRateDelegate setCPUUsageRateDelegate = null;

 

        /// <summary>

        /// CPU 이용률

        /// </summary>

        private int cpuUsageRate = 0;

 

        /// <summary>

        /// 폰트

        /// </summary>

        private Font font = new Font("굴림", 9);

 

        #endregion

               

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            this.setCPUUsageRateDelegate += new SetCPUUsageRateDelegate(SetCPUUsageRate);

 

            this.thread = new Thread(UpdateCPUUsageRate);

 

            this.thread.Start();

        }

 

        #endregion

        #region 폼 닫는 경우 처리하기 - Form_FormClosing(sender, e)

 

        /// <summary>

        /// 폼 닫는 경우 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_FormClosing(object sender, FormClosingEventArgs e)

        {

            this.thread.Abort();

        }

 

        #endregion

        #region CPU 이용률 패널 페인트시 처리하기 - cpuUsageRatePanel_Paint(sender, e)

 

        /// <summary>

        /// CPU 이용률 패널 페인트시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void cpuUsageRatePanel_Paint(object sender, PaintEventArgs e)

        {

            Graphics graphics = e.Graphics;

 

            if(this.cpuUsageRate <= 60)

            {

                graphics.FillRectangle(Brushes.BlanchedAlmond, 0, 0, this.cpuUsageRate, cpuUsageRatePanel.Height);

            }

            else if(this.cpuUsageRate <= 120)

            {

                graphics.FillRectangle(Brushes.Wheat, 0, 0, this.cpuUsageRate, cpuUsageRatePanel.Height);

            }

            else if(this.cpuUsageRate <= 180)

            {

                graphics.FillRectangle(Brushes.NavajoWhite, 0, 0, this.cpuUsageRate, cpuUsageRatePanel.Height);

            }

            else if(this.cpuUsageRate <= 240)

            {

                graphics.FillRectangle(Brushes.Orange, 0, 0, this.cpuUsageRate, cpuUsageRatePanel.Height);

            }

            else

            {

                graphics.FillRectangle(Brushes.DarkOrange, 0, 0, this.cpuUsageRate, cpuUsageRatePanel.Height);

            }

 

            this.cpuUsageRate = this.cpuUsageRate / 3;

 

            graphics.DrawString

            (

                this.cpuUsageRate.ToString() + " %",

                this.font,

                Brushes.DarkRed,

                cpuUsageRatePanel.Width / 2 - 17,

                cpuUsageRatePanel.Height / 4

            );

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region CPU 이용률 갱신하기 - UpdateCPUUsageRate()

 

        /// <summary>

        /// CPU 이용률 갱신하기

        /// </summary>

        private void UpdateCPUUsageRate()

        {

            while(true)

            {

                this.cpuUsageRate = (int)this.performanceCounter.NextValue();

 

                Invoke(setCPUUsageRateDelegate, this.cpuUsageRate);

 

                Thread.Sleep(1000);

            }

        }

 

        #endregion

        #region CPU 이용률 설정하기 - SetCPUUsageRate(cpuUsageRate)

 

        /// <summary>

        /// CPU 이용률 설정하기

        /// </summary>

        /// <param name="cpuUsageRate">CPU 이용률</param>

        private void SetCPUUsageRate(int cpuUsageRate)

        {

            this.Text = string.Format("CPU 이용률 : {0}%", cpuUsageRate);

 

            this.cpuUsageRate = cpuUsageRate * 3;

 

            this.cpuUsageRatePanel.Invalidate();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요