In a C# Program, I am trying to get the CPU usage percentage of the application but it always shows 100

Viewed 1812

Here is my code.

        PerformanceCounter cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

        // will always start at 0
        dynamic firstValue = cpuCounter.NextValue();
        System.Threading.Thread.Sleep(1000);


        dynamic secondValue = cpuCounter.NextValue();
        return secondValue;

There are a lot of people here that are getting the problem that this returns zero, I however am getting 100 at all times, and I know for sure my software is not taking up that much CPU powrer. Any ideas as to what I can do to make this work correctly. To be clear I am trying to retrieve the processor usage from the application running this code not a separate one. Also I am running on a VM but that doesn't seem to matter. Any ideas?

2 Answers
Related