【发布时间】:2018-08-01 05:55:45
【问题描述】:
我按照接受的答案Using PerformanceCounter to Track Memory and CPU Usage
但我发现任务管理器中的结果值不同:
var processName = Process.GetCurrentProcess().ProcessName;
PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", processName);
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", processName);
while (true)
{
Thread.Sleep(500);
double ram = ramCounter.NextValue();
double cpu = cpuCounter.NextValue();
Console.WriteLine("RAM: " + (ram / 1024 / 1024) + " MB; CPU: " + (cpu) + " %");
}
代码正确吗?如何在任务管理器中获得完全相同的值?
【问题讨论】:
标签: c# performance