0

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.

  • This is two questions. Question (1) measure CPU stats without using WMI (2) How do I prevent blocking calls from freezing the UI. They are unrelated and should be asked as separate questions. Please edit your question so that it relates entirely to the title. – spender Sep 16 '16 at 08:45
  • Actually, let's just assume this is a question about measuring CPU without WMI. Now it's a dupe. – spender Sep 16 '16 at 08:47

0 Answers0