gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:03:59 25 4
gpt4 key购买 nike

这是我的代码

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:

最佳答案

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

关于c# - 性能计数器输入字符串的格式不正确 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com