使用PerformanceCounter跟踪每个进程的内存和CPU使用情况?

如何使用System.Diagnostics.PerformanceCounter跟踪进程的内存和CPU使用情况?

对于每个流程数据:

Process p = /*get the desired process here*/; PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName); PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName); while (true) { Thread.Sleep(500); double ram = ramCounter.NextValue(); double cpu = cpuCounter.NextValue(); Console.WriteLine("RAM: "+(ram/1024/1024)+" MB; CPU: "+(cpu)+" %"); } 

性能计数器还有其他计数器,而不是工作集和处理器时间。

我想你想要Windows Management Instrumentation。

编辑:见这里:

测量进程CPU和RAM使用情况

如何在C#中获得CPU使用率?