我想檢索前臺進程(或特定進程)的cpu和ram使用情況。 檢索窗口的標題不是問題,該部分工作。但即使當活動窗口以70%cpu或更高的速度運行時,cpu顯示仍保持爲0%。C#從特定進程(性能計數器)獲取CPU和RAM
(int)pCPU.NextValue(); // < < < <總是返回0 ...
注意:我想用性能計數器來實現。我不想用Process變量來做這件事,因爲那個人可能會提出'不足的特權錯誤'。
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);
public void GetActiveCPUAndRam(out string windowTitle, out int CPUUsagePerc, out int RAMUsage)
{
IntPtr hwnd = GetForegroundWindow();
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
Process activeProc = Process.GetProcessById((int) pid);
#region Window Title
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
if (GetWindowText(hwnd, Buff, nChars) > 0)
windowTitle = Buff.ToString();
else
{
windowTitle = "";
CPUUsagePerc = 0;
RAMUsage = 0;
return;
}
#endregion
#region RAM/CPU
PerformanceCounter pCPU = new PerformanceCounter("Process", "% Processor Time", activeProc.ProcessName, true);
pCPU.NextValue();
CPUUsagePerc = (int)pCPU.NextValue(); // <<<<< problem here.
RAMUsage = 0; // TODO:
#endregion
}
編輯: 我嘗試了新的解決方案: 但是當我運行是推動CPU使用率到100%(單核)一個CPU壓力測試程序。然後下面的解決方案顯示,該進程的CPU使用率是總CPU的300-400%...顯然有些東西仍然是錯誤的。
private PerformanceCounter pCPU = null;
private IntPtr PreviousProcHwnd = IntPtr.Zero;
private CounterSample PreviousCPUCounterSample = CounterSample.Empty;
public void GetActiveCPUAndRam(out string windowTitle, out int CPUUsagePerc, out int RAMUsage)
{
...
#region RAM/CPU
if (PreviousProcHwnd != hwnd)
{
PreviousProcHwnd = hwnd;
pCPU = new PerformanceCounter("Process", "% Processor Time", activeProc.ProcessName,
true);
PreviousCPUCounterSample = CounterSample.Empty;
}
CounterSample sample1 = pCPU.NextSample();
CPUUsagePerc = (int)CounterSample.Calculate(PreviousCPUCounterSample, sample1);
PreviousCPUCounterSample = sample1;
RAMUsage = 0; // TODO:
#endregion
}
你見過http://openhardwaremonitor.org/嗎? – 2012-01-28 19:38:33