如何在 C# 中进行只进、只读 WMI 查询?
一位同事告诉我,如果我的 WMI 系统信息收集查询是只进和/或只读的,那么它们会更快。 这就说得通了。 但我该怎么做呢?
I've been told by a coworker that if my WMI system information gathering queries are forward-only and/or read-only, they'll be quite faster. That makes sense. But how do I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 EnumerationOptions 类并将其 Rewindable 属性设置为 false。 下面是一个示例:
除非您使用它来枚举具有大量实例的类(如 Cim_DataFile),否则您不会看到任何性能改进,并且您只能枚举返回的 ManagementObjectCollection 一次。 您也将无法使用 ManagementObjectCollection.Count 等。
至于只读查询,我不知道如何进行。
You need to use EnumerationOptions class and set its Rewindable property to false. Here is an example:
You won't see any performance improvement unless you use it to enumerate a class with a large number of instances (like Cim_DataFile) and you will get to enumerate the returned ManagementObjectCollection only once. You also won't be able to use ManagementObjectCollection.Count, etc.
As for read-only queries, I'm not sure how to make those.
您的同事一定打算将半同步方法调用与仅向前枚举器一起使用。 在半同步模式下,WMI 方法调用立即返回,对象在后台检索并在创建后按需返回。 另外,当使用半同步模式检索大量实例时,建议获取只进枚举器以提高性能。 这篇MSDN 文章。
正如 Uros 所指出的,要在半同步模式下获取只进枚举器,您需要使用
EnumerationOptions
类实例,并将ReturnImmediately
属性设置为true< /code> 并将
Rewindable
属性设置为false
,例如:Your co-worker must have meant using the semisynchronous method calls together with forward-only enumerators. In the semisynchronous mode, WMI method calls return immediately and objects are retrieved in the background and returned on demand once they are created. Also, when using semisynchronous mode to retrieve a large number of instances, it is recommended to obtain forward-only enumerators to improve the performance. These peculiarities are explained in this MSDN article.
As Uros has pointed out, to get a forward-only enumerator in semisynchronous mode, you need to use the
EnumerationOptions
class instance with theReturnImmediately
property set totrue
and theRewindable
property set tofalse
, e.g.: