我不知道为什么,但许多计算机在执行以下操作时挂起:
void Init()
{
net1 = new List<PerformanceCounter>();
net2 = new List<PerformanceCounter>();
foreach (string instance in new PerformanceCounterCategory("Network Interface").GetInstanceNames())
{
net1.Add(new PerformanceCounter("Network Interface", "Bytes Received/sec", instance));
net2.Add(new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance));
}
}
//Once in 1 second
void UpdateStats()
{
Status.Text = "";
for (int i = 0; i < net1.Count; i++)
Status.Text += string.Format("{0}/{1} Kb/sec; ", net1[i].NextValue() / 1024, net2[i].NextValue() / 1024);
}
Run Code Online (Sandbox Code Playgroud)
在某些计算上,程序在第一次调用 时完全挂起UpdateStats(),而其他计算则经历 100% CPU 负载,但程序运行(缓慢)。其他计数器似乎new PerformanceCounter("Processor", "% Processor Time", "_Total")工作正常。
有什么建议吗?这是为什么?