How can I make the performance counter work without forcing the user to rebuild using lodctr on the command line?

I am trying to get overall CPU usage of a Windows PC (Windows 7 running .Net 4.5) in C #. It seems like using PerformanceCounter should meet my needs.

I wrote some trial code based on the three links below (and checking the msdn pages), it was the simplest version:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace EntropyProject { class Program { static void Main(string[] args) { PerformanceCounter cpuCounter; cpuCounter = new PerformanceCounter(); cpuCounter.CategoryName = "Processor"; cpuCounter.CounterName = "% Processor Time"; cpuCounter.InstanceName = "_Total"; while(true) { try { float firstValue = cpuCounter.NextValue(); System.Threading.Thread.Sleep(500); Console.WriteLine("Before getting processor:"); float currentCpuUsage = cpuCounter.NextValue(); Console.WriteLine("After getting processor:"); System.Threading.Thread.Sleep(1000); Console.WriteLine(currentCpuUsage); } catch (Exception e) { Console.WriteLine("\n{0}\n", e.Message); } System.Threading.Thread.Sleep(10000); } } } } 

Whenever NextValue is called, the exception exception below is raised. This is apparently a common problem with the performance counter performance issue.

Cannot load counter data because an invalid index was read '' from the registry

The most recommended solutions suggest that you repair damaged items using lodctr in a raised command window as an administrator. However, I wanted to use PerformanceCounters in a program that would be released to a large number of people, and so it would be inappropriate to expect them to rebuild their PerformanceCounters using the command window.

Questions:

  • Why does this exception error occur?
  • How to use PerformanceCounter correctly otherwise?
  • How can I avoid users of my programs having to open the cmd window and rebuild their performance counters?

Sources:

A similar error message when accessing the counter name asked by Annie Sheikh

+7
c # performancecounter cpu-usage
source share
1 answer

According to the documentation for PerformanceCounter.NextValue : "You must have administrative privileges to read performance counters." The docs also indicate that a UnauthorizedAccessException will be thrown if "Code that runs without administrative privileges tried to read the performance counter."

But this is a lie. In my Windows 7 environment (64-bit version of Home Premium) running on .NET 4.5, I can just execute your code from an un-raised CMD shell in an administrator account or standard user account or even a guest account.

If you are faced with a privilege problem, then most likely, no number of registry attempts will prevent your code from working reliably for all users. But I don’t understand why your code works fine on a guest account without administrator privileges, given what the documentation says.

Another option is to run "wmic.exe cpu get loadpercentage" and analyze the output as suggested in this answer . Based on my testing, this works either with an administrator account or with a regular user, but not with a guest account.

+1
source share

All Articles