By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

Monitor CPU Usage for 2 processors with active multithreading

P: 4
Hello!

Can somebody give me a hint?

I am trying to obtain the CPU usage for 2 processors (or more ) with active
multithreading. i tried all methods - using the API and using the performance counter and it doesn't work, the results are not like the one's in taskmanager.
I tried getting the _Total or (200 minus Idle time). I will dispose the last try number 7 i guess.

The code is:

PerformanceCounter counter = new PerformanceCounter();
foreach (Process pro in process)
{
counter.CategoryName = "Process";
counter.CounterName = "% Processor Time";
counter.InstanceName = "Idle";
counter.NextValue();
//here i do something to display the MemUsage for some processes
}
System.Threading.Thread.Sleep(1000);
float convert = 200 - counter.NextValue();
TextBox1.Text = convert.ToString();

Can anybody help me please?
Oct 1 '07 #1
Share this Question
Share on Google+
2 Replies


Plater
Expert 5K+
P: 7,872
Can this be done with WMI?
How would you get performance of the 2 cpu's manually? Like as a human, what would you do to find out?

The values in taskmanger are heavily computed (like a lot of computations are done to produce those numbers I think)
Oct 1 '07 #2

P: 4
Yes, you are right. This can be done with WMI. Thank you so much :). I think i solved it:
I only have one more question: there are 2 classes - Win32_Processor - the one that i used - and Win32_PerfFormattedData_PerfProc_Process.

I tried solving this with the second one, and the values were always 0 or 100. I found something on the web, but only for scripts. Is there a difference of performance between the 2 classes?

The code is:
ConnectionOptions connection2 = new ConnectionOptions();
ManagementScope ms1 = new ManagementScope("\\\\localhost", connection2);
ObjectQuery query1 = new ObjectQuery("Select LoadPercentage, DeviceID, Name from Win32_Processor");
ManagementObjectSearcher searcher2 = new ManagementObjectSearcher(ms1, query1);
ManagementObjectCollection returncollection2 = searcher2.Get();
ArrayList processor = new ArrayList();
foreach (ManagementObject returnc in returncollection2)
{
processor.Add(returnc["LoadPercentage"].ToString());
processor.Add(returnc["DeviceID"].ToString());
processor.Add(returnc["Name"].ToString());
}

This works ok, but I found a property in the second class - PercentUserTime - that might do the trick. I can't get this thing to work, shows only 0 or 100.
Any sugestions?
Oct 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.