WMI LoadPercentage 调用非常慢

发布于 2024-10-27 15:37:22 字数 773 浏览 0 评论 0原文

我正在尝试使用 WMI 来检查我的 CPU 负载(Xeon,4 核)。我运行的是 XP 64 (SP2)。

private void button1_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> cpuInfo = new Dictionary<string, string>();


        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            cpuInfo.Add("LoadPercentage"+queryObj["DeviceID"].ToString(), queryObj["LoadPercentage"].ToString());
        }

        foreach (KeyValuePair<string,string> kvp in cpuInfo)
        {
            richTextBox1.AppendText(String.Format("{0} {1}\n", kvp.Key, kvp.Value));
        }
    }

上面的例子运行起来,但是非常慢,大约需要5秒。这是 WMI 问题还是我做错了什么?

I'm trying to use WMI for checking on my CPU load (Xeon, 4 cores). I'm running XP 64 (SP2).

private void button1_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> cpuInfo = new Dictionary<string, string>();


        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            cpuInfo.Add("LoadPercentage"+queryObj["DeviceID"].ToString(), queryObj["LoadPercentage"].ToString());
        }

        foreach (KeyValuePair<string,string> kvp in cpuInfo)
        {
            richTextBox1.AppendText(String.Format("{0} {1}\n", kvp.Key, kvp.Value));
        }
    }

The example above runs, but is very slow, about 5 seconds. Is that a WMI problem or am I doing something fundamentally wrong?

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

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

发布评论

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

评论(1

八巷 2024-11-03 15:37:22

好的,我提供这个作为答案,尽管它可能不是 - 我仍然发现它不适合单独发表评论。

我在 Windows XP SP3 和 Windows 7 上也遇到同样的问题。一旦我在 WMI 查询中包含 LoadPercentage 属性,每个物理处理器(即 Win32_Processor 的实例)就会运行大约 1 秒。代码>)。反之亦然:如果我在查询中包含所有属性,但不包含 LoadPercentage 属性,则它会在几毫秒内运行。

可以使用多种工具、自定义工具或提供的操作系统(即 wmic cpu)观察到此行为。

我只能推测原因是什么,它可能与LoadPercentage 的定义(强调已添加):

每个处理器的负载能力,最后一秒的平均值。
处理器负载是指每个处理器的总计算负担
处理器一次。该属性继承自 CIM_Processor。

WMI 提供程序的实现实际上可能需要 1 秒来对调用时“平均到最后一秒”的负载百分比进行采样。我猜想提供者会以某种方式在后台执行此操作,并且只返回调用时的内容。但无论如何,这只是猜测。

更新:FWIW,Windows 8 开发者预览版似乎显示了相同的行为。

OK, I provide this as an answer, although it might not be - still I find it not fitting for a comment alone.

I have the same issue on Windows XP SP3 and Windows 7. As soon as I include the LoadPercentage property in a WMI query it runs about 1 second per physical processor (i.e. instance of Win32_Processor). The opposite is also true: if I include all properties in the query, but don't include the LoadPercentage property, it runs in a couple of milliseconds.

This behaviour can be observed with several tools, custom or OS provided (i.e. wmic cpu).

I can only speculate what the reason is, it might be related to the definition of LoadPercentage (emphasis added):

Load capacity of each processor, averaged to the last second.
Processor loading refers to the total computing burden for each
processor at one time. This property is inherited from CIM_Processor.

The implementation of the WMI provider might actually need that 1 second to sample the load percentage "averaged to the last second" at the time you make the call. I would have guessed that the provider somehow does that in the background and only returns what it has at the time of the call. But anyhow, that is just speculation.

UPDATE: FWIW, the Windows 8 Developer Preview seems to show the same behavior.

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