PerformanceCounter.NextValue 在某些计算机上挂起

发布于 2024-09-02 07:15:01 字数 913 浏览 3 评论 0原文

我不知道为什么,但许多计算机会因以下操作而挂起:

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);
}

在某些计算机上,程序在第一次调用 UpdateStats() 时完全挂起,而其他计算机则经历 100% CPU 负载,但程序运行(缓慢)。其他计数器,如 new PerformanceCounter("Processor", "% Processor Time", "_Total") 似乎工作正常。

有什么建议吗?这是为什么?

I don't know why, but many computers hangs on following operation:

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);
}

On some computes program hangs completely on first call of UpdateStats(), others experiencing 100% CPU load but program works (slowly). Other counters like new PerformanceCounter("Processor", "% Processor Time", "_Total") seems to work fine.

Any suggwstions why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2024-09-09 07:15:01

删除打印机引用有助于消除初始挂起

  new PerformanceCounter("Processor", "% Processor Time", "_Total").  

现在我们收到一条新的错误消息而不是挂起,因此可以跟踪它。来自:

有关打印机驱动程序的解答

我们有两台类似的戴尔笔记本电脑(一台较新),均装有 Windows 8、MVSV 2013 和 IIS Express 8. 一个会挂,一个不会。

Removing printer references helped eliminate a hang on the initial

  new PerformanceCounter("Processor", "% Processor Time", "_Total").  

Now we get a fresh error message instead of hang so can trace it. From:

Answer about printer drivers

We had two similar dell laptops (one newer), both with Windows 8, MVSV 2013, and IIS Express 8. One would hang, one would not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文