1.首先先引用对应的命名空间
using System.Diagnostics;
2.接着定义一个监测CPU的对象:
private PerformanceCounter m_Cpu = null;
3.在界面加载的时候获取对应的CPU对象
string strProcessName = Process.GetCurrentProcess().ProcessName;
try
{
m_Cpu = new PerformanceCounter("Process", "% Processor Time", strProcessName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
4.在控件上面显示
if (m_Cpu != null)
{
double cpu = m_Cpu.NextValue();
label1.Text = "CPU: " + cpu.ToString("F1") + "%";
}