I follow the accepted answer Using PerformanceCounter to Track Memory and CPU Usage
but I've found that the result is not same value in Task Manager:
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) + " %");
}
Is the code correct? How can I get the exactly same value in Task Manager?