性能计数器输入字符串的格式不正确 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); }
我对其进行了尝试和捕获,这是它抛出的异常输入字符串的格式不正确
。
它通过打印前两个消息框,然后抛出异常。
现在我无法理解的是,它可以在 Windows 7 Ultimate 和 Windows Server 2003 上运行,但在我的同事 Windows 7 Ultimate 上却失败了。它曾经在他的计算机上运行,但突然停止了。近期历史中唯一的区别是他改变了他的网络。
编辑
Framework 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:
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 a try and catch around it and here is the exception it throws Input string was not in a correct format
.
It gets through printing the first two Message Boxes, And then throws the exception.
Now here is what I cant get my head around, It works on Windows 7 Ultimate and Windows Server 2003, but on my colleagues Windows 7 Ultimate it Fails. It use to work on his computer, but suddenly its just stopped. The only difference in the near history has been that he has changed his Network.
Edit
Framework 4 was updated this morning. But I have it on my machine as well, so if it was the framework issue why is it not occurring on mine
Any ideas cause im lost
Tested on his machine
Its failing at the first 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您可能会发现这是性能计数器无法正常工作的问题。如果您打开 perfmon(开始>运行>Perfmon)并看到类似这样的错误。
然后,您需要从 Windows\System32 文件夹中的 PerfStringBackup.ini 重建计数器。请按照此处获取更多信息:http://drayblog.gotdns.com/index.php/2010/02/04/diagnostics-performancecounter-input-string-was-not-in-a- Correct-format/< /a>.
I think you may find that this is an issue with the performance counters not working correctly. If you open perfmon (Start>Run>Perfmon) and see an error like this.
Then you will need to rebuild the counters from the PerfStringBackup.ini in the Windows\System32 folder. Follow this here for more info: http://drayblog.gotdns.com/index.php/2010/02/04/diagnostics-performancecounter-input-string-was-not-in-a-correct-format/.
您同事的计算机上的(口语/书面)语言是否有可能与您的设置不同,或者他们篡改了该计算机上的任何本地化设置?这通常是 .NET 程序中微妙错误的来源,特别是在将字符串与不同格式相互转换时......
Any chance the (spoken/written) language on your co-worker's machine is set differently than yours, or that they fiddled with any of the localization settings on that machine? This is often a source of subtle bugs in .NET programs, especially when converting a string to/from different formats...