如何在WMI查询上设置超时?

| 我有一个.NET应用程序,该应用程序在所有域计算机上运行WMI查询,以便找到登录的用户。它会对每台计算机执行ping操作以查找其是否在线,然后运行实际的查询。 程式码片段:
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查询会无限期挂起。 如何设置超时时间?     
已邀请:
        ManagementObjectSearcher具有
Options
属性:可用选项之一是
Timeout
,类型为
TimeSpan
:   获取或设置要应用于的超时   操作。请注意   返回集合的操作,   此超时适用于   通过结果枚举   集合,而不是操作本身   (ReturnImmediately属性为   用于后者)。该属性是   用来表示该操作   应该执行   半同步。     
        试试
co.Timeout = new TimeSpan(0, 0, 30);
    

要回复问题请先登录注册