How can I use System.Diagnostics.PerformanceCounter to track the memory and CPU usage for a process?
For per process data:
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)+" %");
}
Performance counter also has other counters than Working set and Processor time.
Robert Harvey
I think you want Windows Management Instrumentation.
EDIT: See here:
来源:https://stackoverflow.com/questions/3411805/using-performancecounter-to-track-memory-and-cpu-usage-per-process