2010-06-25 110 views
3

这里是我的代码性能计数器输入字符串的不正确的格式C#

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今早更新。但我有我的机器上一样,所以如果这是它为什么不能在我的

出现的框架问题的任何想法引起IM失去

测试他的机器

其在没有上第一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: 
+0

哦应用程序正在调试模式下进行编译,如果有什么差别 – 2010-06-25 21:53:19

+0

您可以张贴堆栈跟踪吗? – 2010-06-25 21:53:52

+0

那么,究竟是什么样的调用抛出异常呢? – 2010-06-25 21:55:49

回答

7

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

+0

太棒了。我在一个没有文档和支持(也不是源代码)的完全无关的商业程序中遇到了这个问题,并且可以在您的帖子中修复它。 – rottz 2013-02-16 22:33:21

+0

此链接不工作了,所以我建议使用此链接:https://blogs.technet.microsoft.com/yongrhee/2009/10/06/how-to-rebuild-performance-counters-on-windows- vistaserver20087server2008r2/ – Minamoto 2016-03-21 13:50:57

+0

但是,这个建议并没有帮助我,我发现了另一种选择:只需运行一个命令“lodctr/r”,它的工作就完美了。 – Minamoto 2016-03-21 13:57:07

0

您的同事机器上的(说/写)语言的任何机会设置都与您的机器不同,或者它们与该机器上的任何本地化设置混杂在一起?这往往是在.NET程序微妙的错误的来源,一个字符串从不同的格式转换成/特别是当...

+0

Highley unlikley所有其他机器上。但即便如此,如果是这样的话。为什么会a。我在机器上编译的版本,当我复制二进制文件时失败? – 2010-06-26 00:47:26