如何设置 WMI 查询超时?

发布于 2024-11-15 09:53:15 字数 955 浏览 1 评论 0原文

我有一个 .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 查询会无限期地挂起。

我该如何设置它的超时时间?

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 snippet:

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
}

The problem: sometimes the WMI query hangs on indefinitely.

How can I set a timeout on it?

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

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

发布评论

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

评论(2

残月升风 2024-11-22 09:53:15

ManagementObjectSearcher 有一个选项< /a> 属性:可用选项之一是 超时,类型为TimeSpan

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

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

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.

贵在坚持 2024-11-22 09:53:15

尝试 co.Timeout = new TimeSpan(0, 0, 30);

Try co.Timeout = new TimeSpan(0, 0, 30);

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