【问题标题】:Performance Counter Input string was not in a correct format C#性能计数器输入字符串的格式不正确 C#
【发布时间】:2010-06-25 21:51:56
【问题描述】:

这是我的代码

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 Ultimate 和 Windows Server 2003,但在我的同事 Windows 7 Ultimate 上却失败了。它曾经在他的电脑上工作,但突然停止了。近期历史的唯一不同是他改变了他的网络。

编辑

今天早上更新了框架 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: 

【问题讨论】:

  • 哦,如果这有什么不同的话,应用程序正在调试模式下编译
  • 你能发布堆栈跟踪吗?
  • 那么,究竟是什么调用引发了异常?
  • 目前还没有,编译器在我的机器上,我正在远程连接到他的机器上。对不起,我对堆栈跟踪有点不熟悉
  • gencha - 我猜cpuCounter.NextValue(); 是有问题的地方,但它适用于我测试过的所有其他机器

标签: c# wmi


【解决方案1】:

我想您可能会发现这是性能计数器无法正常工作的问题。如果您打开 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/

【讨论】:

  • 太棒了。我在一个完全不相关的商业程序上遇到了这个问题,它没有文档和支持(也没有源代码),可以用你的帖子解决它。
  • 这个链接失效了,推荐使用这个链接:blogs.technet.microsoft.com/yongrhee/2009/10/06/…
  • 但是这个建议对我没有帮助,我找到了另一种选择:只需运行命令“lodctr /r”,它就可以完美运行。
【解决方案2】:

您同事机器上的(口语/书面)语言设置是否与您的不同,或者他们修改了该机器上的任何本地化设置?这通常是 .NET 程序中细微错误的来源,尤其是在将字符串转换为/从不同格式转换时...

【讨论】:

  • Highley 不太可能。但即便如此,如果是这样的话。为什么会a。我在我的机器上编译的版本,当我复制二进制文件时失败了?
猜你喜欢
  • 2021-11-24
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多