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

时间:2010-06-25 21:51:56

标签: 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); }

我试一试并抓住它,这是它抛出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: 

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

您同事的计算机上的(语音/书面)语言是否有可能与您的语言设置不同,或者他们在该计算机上摆弄任何本地化设置?这通常是.NET程序中微妙错误的来源,尤其是在将字符串转换为不同格式的字符串时...