[C#]透過PerformanceCounter取得特定Process的CPU使用率
var instances = category.GetInstanceNames();
foreach (var instance in instances)
{
using (var counter = new PerformanceCounter(category.CategoryName,
"ID Process", instance, true))
{
int val = (int)counter.RawValue;
if (val == pid)
{
return instance;
}
}
}
throw new ArgumentException("Invalid pid!");
}</pre>
var lastUpdateTime = default(DateTime);
m_UpdateTimePool.TryGetValue(pid, out lastUpdateTime);
var interval = DateTime.Now - lastUpdateTime;
if (interval.TotalSeconds > 1)
{
m_CpuUsagePool[pid] = (int)(m_CounterPool[pid].NextValue() / Environment.ProcessorCount);
}
return m_CpuUsagePool[pid];
}</pre>
public static class ProcessExtension { #region Private Static Var private static Dictionary<int, PerformanceCounter> _counterPool; private static Dictionary<int, DateTime> _updateTimePool; private static Dictionary<int, int> _cpuUsagePool; #endregion
#region Private Static Property
private static Dictionary<int, PerformanceCounter> m_CounterPool
{
get
{
return _counterPool ?? (_counterPool = new Dictionary<int, PerformanceCounter>());
}
}
private static Dictionary<int, DateTime> m_UpdateTimePool
{
get
{
return _updateTimePool ?? (_updateTimePool = new Dictionary<int, DateTime>());
}
}
private static Dictionary<int, int> m_CpuUsagePool
{
get
{
return _cpuUsagePool ?? (_cpuUsagePool = new Dictionary<int, int>());
}
}
#endregion
#region Private Static Method
private static string GetProcessInstanceName(int pid)
{
var category = new PerformanceCounterCategory("Process");
var instances = category.GetInstanceNames();
foreach (var instance in instances)
{
using (var counter = new PerformanceCounter(category.CategoryName,
"ID Process", instance, true))
{
int val = (int)counter.RawValue;
if (val == pid)
{
return instance;
}
}
}
throw new ArgumentException("Invalid pid!");
}
private static int GetCpuUsage(int pid)
{
if (!m_CounterPool.ContainsKey(pid))
{
m_CounterPool.Add(pid, new PerformanceCounter("Process", "% Processor Time", GetProcessInstanceName(pid)));
}
var lastUpdateTime = default(DateTime);
m_UpdateTimePool.TryGetValue(pid, out lastUpdateTime);
var interval = DateTime.Now - lastUpdateTime;
if (interval.TotalSeconds > 1)
{
m_CpuUsagePool[pid] = (int)(m_CounterPool[pid].NextValue() / Environment.ProcessorCount);
}
return m_CpuUsagePool[pid];
}
#endregion
#region Public Static Method
public static string GetInstanceName(this Process process)
{
return GetProcessInstanceName(process.Id);
}
public static int GetCpuUsage(this Process process)
{
return GetCpuUsage(process.Id);
}
#endregion
}
namespace WindowsFormsApplication24 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
lbxProcess.DisplayMember = "ProcessName";
lbxProcess.DataSource = Process.GetProcesses();
}
private void lbxProcess_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedProcess = lbxProcess.SelectedItem as Process;
if (selectedProcess == null)
return;
timer1.Enabled = false;
tbxInstanceName.Text = selectedProcess.GetInstanceName();
tbxCPU.Text = selectedProcess.GetCpuUsage().ToString();
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
var selectedProcess = lbxProcess.SelectedItem as Process;
if (selectedProcess == null)
return;
tbxCPU.Text = selectedProcess.GetCpuUsage().ToString();
}
}
}