如何设置WMI查询超时? [英] How to set a timeout on WMI queries?

查看:207
本文介绍了如何设置WMI查询超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它运行的WMI查询所有域的计算机上,以便找到登录的用户.NET应用程序;它坪每台电脑发现无论是在线与否,然后运行实际的查询。

I have a .NET application which runs WMI queries on all domain computers in order to find the logged in user; it pings each computer to find whether it is online or not, then runs the actual query.

code片断:

try
{
    string loggedonuser = null;

    string computername = "ComputerToQuery";

    ConnectionOptions co = new ConnectionOptions();

    co.Username = "DOMAIN\MyUser";
    co.Password = "MyPassword";

    co.Impersonation = ImpersonationLevel.Impersonate;
    co.Authentication = AuthenticationLevel.Default;

    ManagementPath mp = new ManagementPath(@"\\" + computername + @"\root\cimv2");

    ManagementScope ms = new ManagementScope(mp,co);

    ms.Connect();

    ObjectQuery oq = new ObjectQuery("SELECT username FROM Win32_ComputerSystem");

    ManagementObjectSearcher mos = new ManagementObjectSearcher(ms,oq);

    foreach(ManagementObject mo in mos.Get())
        loggedonuser = (String) mo["username"];
}
catch(Exception e)
{
    // Handle WMI exception
}

问题:有时WMI查询挂起无限期

The problem: sometimes the WMI query hangs on indefinitely.

我怎么能在其上设置超时?

How can I set a timeout on it?

推荐答案

该ManagementObjectSearcher有一个<一个href="http://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher.options.aspx"><$c$c>Options属性:可用选项之一是<一个href="http://msdn.microsoft.com/en-us/library/system.management.managementoptions.timeout.aspx"><$c$c>Timeout,类型时间跨度

The ManagementObjectSearcher has an Options property: one of the available options is Timeout, of type TimeSpan:

获取或设置超时申请   操作。注意,对于   操作返回的集合,   此超时适用于   通过由此而来枚举   集,而不是操作本身   (该ReturnImmediately属性   用于后者)。此属性   用来表示操作   应执行   半同步进行。

Gets or sets the time-out to apply to the operation. Note that for operations that return collections, this time-out applies to the enumeration through the resulting collection, not the operation itself (the ReturnImmediately property is used for the latter). This property is used to indicate that the operation should be performed semi-synchronously.

这篇关于如何设置WMI查询超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆