Performance counter string was not in the correct C # format

Here is my code

float cpuLoad = 0;

try{
    MessageBox.Show("Running");

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

    MessageBox.Show("Performance Counter Created");

    cpuLoad = cpuCounter.NextValue();
    System.Threading.Thread.Sleep(1000);
    cpuLoad = cpuCounter.NextValue();

    MessageBox.Show("Clock Speed Gathered");

    //Remaining Code
}
catch(Exception ex) { MessageBox.Show(ex.Message); }

I have an attempt to catch him, and here it throws an exception Input string was not in a correct format.

It prints the first two message boxes, and then throws an exception.

Now here's what I can’t get around, it works on Windows 7 Ultimate and Windows Server 2003, but it doesn’t work on my Windows 7 Ultimate colleagues. He used to work on his computer, but suddenly he was just stopped. The only difference in the next story was that he changed his Network.

Edit

Box 4 was updated this morning. But I have it on my machine too, so if it was a problem with the frame, why doesn’t it happen on my

Any ideas make me lose

Tested on his car

First failure cpuLoad = cpuCounter.NextValue()

Stack trace

System.FormatException was unhandled
  Message="Input string was not in a correct format."
  Source="mscorlib"
  StackTrace:
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at System.Diagnostics.PerformanceCounterLib.GetStringTable(Boolean isHelp)
       at System.Diagnostics.PerformanceCounterLib.get_NameTable()
       at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()
       at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists)
       at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)
       at System.Diagnostics.PerformanceCounter.Initialize()
       at System.Diagnostics.PerformanceCounter.NextSample()
       at System.Diagnostics.PerformanceCounter.NextValue()
       at CounterTest.Form1..ctor() in C:\Users\x\Documents\x\Applications & Software Development\C# Projects\CounterTest\CounterTest\Form1.cs:line 35
       at CounterTest.Program.Main() in C:\Users\x\Documents\x\Applications & Software Development\C# Projects\CounterTest\CounterTest\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
+3
2

, , . perfmon ( > > ) , .

---------------------------
Performance Monitor Control
---------------------------
Unable to add these counters:

\Memory\Available MBytes
\Memory\% Committed Bytes In Use
\Memory\Cache Faults/sec
\Memory\Cache Faults/sec
\PhysicalDisk(*)\% Idle Time
\PhysicalDisk(*)\Avg. Disk Queue Length
\Network Interface(*)\Bytes Total/sec

PerfStringBackup.ini Windows\System32. : http://drayblog.gotdns.com/index.php/2010/02/04/diagnostics-performancecounter-input-string-was-not-in-a-correct-format/.

+7

, (/) , , ? .NET-, / ...

0

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


All Articles