분노의 챔질
[C#] CPU 사용량 보기 본문
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Management;
namespace CPUUsage
{
public partial class Form1 : Form
{
private Thread addDataRunner;
public delegate void AddDataDelegate();
public AddDataDelegate addDataDel;
PerformanceCounter p;
public Form1()
{
InitializeComponent();
}
private void btnShow_Click(object sender, EventArgs e)
{
p = new PerformanceCounter();
p.CategoryName = "Processor";
p.CounterName = "% Processor Time";
p.InstanceName = "_Total";
addDataRunner.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
ThreadStart addDataThreadStart = new ThreadStart(AddDataThreadLoop);
addDataRunner = new Thread(addDataThreadStart);
addDataDel += new AddDataDelegate(AddData);
progressBar1.Maximum = 100;
}
private void AddDataThreadLoop()
{
while (true)
{
progressBar1.Invoke(addDataDel);
Thread.Sleep(1000);
}
}
public void AddData()
{
float usage = p.NextValue();
progressBar1.Value = ((int)usage);
lbUsage.Text = "CPU사용 : " + ((int)usage).ToString() + "%";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
addDataRunner.Abort();
}
}
}
'Programming > C#' 카테고리의 다른 글
[C#] 요일구하기 (1) | 2010.03.26 |
---|---|
[C#] 드라이브 정보보기 DriveInfo (0) | 2010.03.23 |
[C#] 붙여넣기, Ctrl+V (0) | 2010.03.09 |
[C#] 간단한 UserControl (0) | 2010.03.05 |
[C#] IP주소 알아내기 (0) | 2010.03.05 |