Process CPU Usage

  • Thread starter Thread starter Mohammad-Reza
  • Start date Start date
M

Mohammad-Reza

Hi
Please tell me how I can find that a process CPU usage like TaskManager.

Thanks.
 
Hi Mohammad-Reza,

You can use PerformanceCounter objects for this. It's been a while since I used them, so I won't be able to tell you exactly how to use them right now, but I would imagine something like

PerformanceCounter counter = new PerformanceCounter("Process", "% Processor Time", "MyProgram");

You can obtain values using counter.NextValue()
 
Great but where I get full list of categories?

I finded in MSDN but I didn't see all categories.

Morten Wennevik said:
Hi Mohammad-Reza,

You can use PerformanceCounter objects for this. It's been a while since
I used them, so I won't be able to tell you exactly how to use them right
now, but I would imagine something like
 
ok ... find it :D Server Explorer.

but it's only way for retrieving this... there aren't documentation online
about full list?

SnakeS said:
Great but where I get full list of categories?

I finded in MSDN but I didn't see all categories.

Morten Wennevik said:
Hi Mohammad-Reza,

You can use PerformanceCounter objects for this. It's been a while
since
I used them, so I won't be able to tell you exactly how to use them right
now, but I would imagine something like
 
Well, one way is to start performance viewer in (Control Panel/Programs)->Administrative Tools->Performance
Right click the graph and select "Add Counters"

"Performance Object" should be the same as CategoryName,
"Select counters from list" is the same as CounterName, and
"Select Instances from List" the same as InstanceName.
 
Thanks for your helps guys.
I did them before but when i use NextValue() method in a MessageBox its work perfectly but when I list processes in a ListView it doesn't work. I write the codes for you.


Process[] PrsArr = Process.GetProcesses();
PerformanceCounter PCounter = new PerformanceCounter();
PCounter.CategoryName = "Process";
PCounter.CounterName = "% Processor Time";
int i = 0;
foreach( Process Prs in PrsArr )
{
LV.Items.Add(Prs.ProcessName);
PCounter.InstanceName = Prs.ProcessName;
LV.Items.SubItems.Add(PCounter.NextValue().ToString());
LV.Items.SubItems.Add(string.Format("{0:0#}:{1:0#}:{2:0#}",Prs.PrivilegedProcessorTime.Hours,
Prs.PrivilegedProcessorTime.Minutes, Prs.PrivilegedProcessorTime.Seconds));
LV.Items.SubItems.Add( string.Format("{0:#,###}",(Prs.WorkingSet / 1000)) + "k" );
i++;
}

Its make the Coulmn 2 for all items to zero.
 
I don't think you can reuse a performance counter to keep track of multiple instances.

This sample is a bit of a hack (wmiapsrv kept causing errors) but will report cpu usage for the instances.

Loaded once, for instance in the Load event

counters = new ArrayList();
Process[] PrsArr = Process.GetProcesses();
foreach(Process p in PrsArr)
{
if(p.ProcessName == "wmiapsrv")
continue;
PerformanceCounter PCounter = new PerformanceCounter();
PCounter.CategoryName = "Process";
PCounter.CounterName = "% Processor Time";
PCounter.InstanceName = p.ProcessName;
counters.Add(PCounter);
LV.Items.Add(p.ProcessName).SubItems.Add("0");
}

Executed on each timer event

for(int i = 0; i < counters.Count; i++)
{
PerformanceCounter Prs = (PerformanceCounter)counters;
LV.Items.SubItems[1].Text = ((int)Prs.NextValue()).ToString();
}
 
Back
Top