I'm going to get CPU Usage per core in a lot of computers that have multi SMPs and multi cores in each SMP. some of them are hyperthreaded. I used this code but it takes a lot of CPU cycle and time and even not working.
var pc = new PerformanceCounter("Processor Information", "% Processor Time");
var cat = new PerformanceCounterCategory("Processor Information");
var instances = cat.GetInstanceNames();
var cs = new Dictionary<string, CounterSample>();
foreach (var s in instances)
{
pc.InstanceName = s;
cs.Add(s, pc.NextSample());
}
while (true)
{
foreach (var s in instances)
{
pc.InstanceName = s;
coresUCCurListBox.Items.Add(Calculate(cs[s], pc.NextSample()).ToString());
cs[s] = pc.NextSample();
}
System.Threading.Thread.Sleep(500);
}
I prefer not to use Performance counters or WMI because they take 6.5 seconds to process. I want to monitor CPU Usage each second. and another thing is that this code runs with the UI thread and my Form freezes while running. Thanks for help in advance.