性能计数器输入字符串的格式不正确C#

Ang*_*.47 3 c# wmi

这是我的代码

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); }
Run Code Online (Sandbox Code Playgroud)

我试试并抓住它,这是它抛出的例外Input string was not in a correct format.

它通过打印前两个消息框,然后抛出异常.

现在我无法理解这一点,它可以在Windows 7旗舰版和Windows Server 2003上运行,但在我的同事Windows 7旗舰版它失败了.它用于在他的计算机上工作,但它突然停止了.近历史上唯一的不同就是他改变了自己的网络.

编辑

框架4今天上午更新.但我也在我的机器上也有它,所以如果它是框架问题,为什么它不会发生在我的机器上

任何想法都会导致我迷失

在他的机器上测试过

它在第一次失败 cpuLoad = cpuCounter.NextValue()

堆栈跟踪

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: 
Run Code Online (Sandbox Code Playgroud)

aHu*_*ter 8

我想您可能会发现这是性能计数器无法正常工作的问题.如果你打开perfmon(开始>运行> 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
Run Code Online (Sandbox Code Playgroud)

然后,您需要从Windows\System32文件夹中的PerfStringBackup.ini重建计数器.请点击此处获取更多信息:http://drayblog.gotdns.com/index.php/2010/02/04/diagnostics-performancecounter-input-string-was-not-in-a-correct-format/.