Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the CPU usage generates "Category does not exist" error

Tags:

c#

I'm using the flowing code, but it says that category does not exist.

static PerformanceCounter cpuUsage;

public static void Main(string[] args)
{
    cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total");

    Console.WriteLine(cpuUsage.NextValue() + " %");
    Thread.Sleep(1000);
    Console.WriteLine(cpuUsage.NextValue() + " %");
    Console.Read();
}
like image 524
Ashekur Rahman molla Asik Avatar asked Sep 15 '12 08:09

Ashekur Rahman molla Asik


2 Answers

Good afternoon!

The core cause of this problem is a seemingly random corruption of the pointers to the performance counters in the registry. This happens infrequently, but most often happens on Windows Server 2008 R2.

Strictly speaking the "Process" and "Processor" category should always exist by default as performance counters. If they are missing, there are possibly many other counters that are missing as well. The previous solutions would not solve the problem if the "Processor Information" counter was also corrupted as well. To resolve this definitively you can run the following command:

lodctr /R

This will repair any broken pointers to your counters. To verify this solution you can go to Server Manager -> Monitoring -> Performance Monitor -> Add ... Within this view you can view all of the currently registered performance counters. Both "Processor" and "Process" should now be available. Alternatively, you can run the following command to view the status of all of the available counters as well:

lodctr /Q

As a side note, this command should be run from an Administrative console; otherwise this process may fail with "error code: 5 (Access Denied)"

like image 68
Brian Gilreath Avatar answered Oct 14 '22 11:10

Brian Gilreath


Use

new PerformanceCounter("Processor Information", "% Processor Time", "_Total");

Instead of

new PerformanceCounter("Processor", "% Processor Time", "_Total");
like image 20
Ashekur Rahman molla Asik Avatar answered Oct 14 '22 10:10

Ashekur Rahman molla Asik