WMI LoadPercentage 调用非常慢
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我提供这个作为答案,尽管它可能不是 - 我仍然发现它不适合单独发表评论。
我在 Windows XP SP3 和 Windows 7 上也遇到同样的问题。一旦我在 WMI 查询中包含 LoadPercentage 属性,每个物理处理器(即 Win32_Processor 的实例)就会运行大约 1 秒。代码>)。反之亦然:如果我在查询中包含所有属性,但不包含 LoadPercentage 属性,则它会在几毫秒内运行。
可以使用多种工具、自定义工具或提供的操作系统(即
wmic cpu
)观察到此行为。我只能推测原因是什么,它可能与
LoadPercentage
的定义(强调已添加):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 ofWin32_Processor
). The opposite is also true: if I include all properties in the query, but don't include theLoadPercentage
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):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.