[ACCEPTED]-Getting CPU usage of a process in C#-c#

Accepted answer
Score: 11

Actually, the Process\% Processor Time\Instance 14 counter returns the % of time that the monitored process uses on % User time for a single processor. So the limit is 100% * the number of processors you have.

There doesn't seem to be an easy 13 way to compute the value that taskmgr displays 12 using perfmon counters. See this link.

Also remember 11 the percentage of CPU usage is not a fixed 10 value, but a calculated value:

((total processor time at time T2) - (total processor time at time T1) / (T2 - T1))

This means 9 that the values depend on both T2 and T1, so 8 there might be differences between what 7 you see on task manager and what you compute, if 6 T2 and T1 used by Task Manager are slightly 5 different than T2 and T1 used by your program.

If 4 you are interested, I can provide you some 3 code to retrieve this value using P/Invoke. But'll 2 loose the benefits of Performance Counters 1 (such as monitoring remote processes).

Score: 11

Dividing by the processor/core count is what seemed to yield fairly accurate 2 results when comparing against Task Manager.

To 1 save people time:

// This will return the process usage as a percent of total processor utilisation.
var processUsage = process_cpu_usage/nextValue() / Environment.ProcessorCount;

More Related questions