Getting CPU Usage Generates Category Not Existing Error

I use the current code, but it says that the 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(); } 
+4
source share
4 answers

His work is beautiful at my end. Look at the image.

0
source

Good afternoon!

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

Strictly speaking, the categories Process and Processor should always exist by default as performance counters. If they are missing, there may be many other counters that are also missing. Previous solutions do not solve the problem if the processor information counter has also been damaged. To finally solve this problem, you can run the following command:

 lodctr /R 

This will repair any broken pointers to your counters. To check this solution, you can go to Server Manager → Monitoring → Performance Monitor → Add ... In this view, you can view all 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 available counters:

 lodctr /Q 

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

+8
source

Use

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

Instead

 new PerformanceCounter("Processor", "% Processor Time", "_Total"); 
+6
source

Use this static method ( MSDN ):

 PerformanceCounterCategory.GetCategories() 

to get an array of all categories registered on your computer. Processor been localized.

See this SO answer for code that retrieves all counters.

0
source

Source: https://habr.com/ru/post/1434338/


All Articles