Discrepancy in Formatted PercentProcessorTime values


Al Cadalzo

I'm only getting values of either 0 or 100 for PercentProcessorTime from
..Net System.Management classes:

private int _instanceCounter = 0;
private int _flaggedCounter = 0;
private int _cpuPercent = 0;
private long _memory = 0;

string wql = "select * from win32_PerfFormattedData_PerfProc_Process where
name = 'TargetApp'";
string scope = @"\\" + serverName + @"\root\cimv2";
_mgmtObjSearcher = new ManagementObjectSearcher(scope,wql);
_alertCpuPercent = alertCpuPercent;

foreach (ManagementObject inst in _mgmtObjSearcher.Get())
_cpuPercent = Convert.ToInt32(inst["PercentProcessorTime"]);
_memory = Convert.ToInt64(inst["WorkingSet"])/1024;

Console.WriteLine("{0} Instance {1} CPU: {2}% Mem:

_cpuPercent,_memory );


The above runs every second. It fluctuates between 0 and 100 on a running

However if I do it from scripting then I get values closely resembling what
TaskManager shows on the Processes tab.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
set PerfProcess = objWMIService.Get(_

While (True)
Wscript.Echo PerfProcess.PercentProcessorTime
Wscript.Sleep 1000

Why am I only getting either 0 or 100 when I use .Net Management classes,
but when I use scripting I get values of 2 or 3%

which seem to track what I am seeing in TaskManager. I would like to get
the same values using .Net Management classes as I do using WMI scripting.

My O/S is WindowsXP SP2. Performance counter instances for Processor > %
Processor Time are '_Total', '0' and '1'.

Oct 28, 2009
Reaction score

The key appears to be in the Refresh_ -- looks like it only calculates cpu usage "since the last refresh" (or instantaneous on creation)

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question