【发布时间】: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();是有问题的地方,但它适用于我测试过的所有其他机器