5

In a windows application, how do I get the TOTAL current CPU usage of ALL processes (not just one process or application) on ALL cores in C#? (without using any third party libraries, if possible)

I could only find questions and answers about the CPU usage of a single process. For example this one appears to ask for the CPU usage of just the running program, and the answers are unclear about what information their code gathers, so it's not very helpful in this instance.

Though, if this question is answered somewhere else, I will happily accept a link.

Thank you!

Danegraphics
  • 447
  • 1
  • 7
  • 21
  • 1
    You'll need to use the `PerformanceCounter` class alongside a loop to retrieve all processes – Sasha Jul 05 '18 at 13:53
  • 3
    Possible duplicate of [How to get the CPU Usage in C#?](https://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c) – Camilo Terevinto Jul 05 '18 at 13:55
  • If you have the single process figured out then the next step should be as easy as finding all running processes, then getting all the usage... – Michael Puckett II Jul 05 '18 at 13:55
  • @CamiloTerevinto - It does not answer my question, as that is asking about only one application, not ALL of them total. – Danegraphics Jul 05 '18 at 14:11
  • 1
    Try it out, the answers with `("Processor", "% Processor Time", "_Total");` should be the total CPU (yes, the answer is not what the question asks for) – Camilo Terevinto Jul 05 '18 at 14:12
  • @CamiloTerevinto - I'll try that. If it works, then it should be posted as the answer to this question, not that one, as this question is not a duplicate even if it may be answered in another question. – Danegraphics Jul 05 '18 at 14:14
  • @Danegraphics: It is a duplicate: the other question asks for both the per-process CPU and the Total CPU. The request for (and answers for) Total CPU completely cover your question. – Ben Voigt Jul 05 '18 at 14:30
  • 1
    @BenVoigt - Given the poorly worded nature of the question, it does not appear to ask my question, even with a full reading. On top of that, the answers do not appear to discern between the two. Therefore, it would not be clear to someone asking my question that the answer is in that question. In fact, quite the opposite impression is given. Duplicates are questions that are clearly asked elsewhere, not questions that may find unclear answers in a seemingly unrelated question. – Danegraphics Jul 05 '18 at 14:45
  • @BenVoigt - It's also better to keep two different questions separate, instead of putting both into a single question thread. – Danegraphics Jul 05 '18 at 18:09
  • @BenVoigt - I can't believe this has essentially devolved into "no u". – Danegraphics Jul 05 '18 at 18:31
  • Look, I've explained how your question can be understood differently from (I understand from the answer you chose) what you were looking for. If you don't care to make it clearer, I don't care. – Ben Voigt Jul 06 '18 at 01:05

2 Answers2

8

When you use the PerformanceCounter class the first call of the NextValue() method most likely will return 0. So, you should call it a few times after some delay to get an appropriate measure.

you will need:

using System.Diagnostics;
using System.Threading;

Then you can obtain it as follows:

static void Main(string[] args)
{
    var cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    Thread.Sleep(1000);
    var firstCall = cpuUsage.NextValue();

    for (int i = 0; i < 5; i++)
    {
        Thread.Sleep(1000);
        Console.WriteLine(cpuUsage.NextValue() + "%");
    }

    Console.Read();
}
L_J
  • 2,351
  • 10
  • 23
  • 28
  • 1
    That first call of `NextValue()` thing was super helpful! Thank you! – Danegraphics Jul 05 '18 at 14:45
  • The values look like approx the half in comparison with task manager on my computer.. Strange. – honzakuzel1989 Feb 14 '21 at 20:39
  • Also seeing this, not sure what is going on... – jjxtra Apr 24 '22 at 14:09
  • 2
    Don't remember where I found this or how it works under the hood, but using "Processor Information", "% Processor Utility" as the first two parameters in the PerformanceCounter constructor makes it match what you see in task manager. – Eric H Jul 27 '22 at 20:00
0
using (PerformanceCounter system_cpu_usage = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
            {
                var first = system_cpu_usage.NextValue();
                Thread.Sleep(2500);
                // Do something
                
            }

You MUST include a var first = system_cpu_usage.NextValue(); in order to then proceed with displaying the actual value.

Nav E
  • 11
  • 3