如何设置 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 查询会无限期地挂起。
我该如何设置它的超时时间?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ManagementObjectSearcher 有一个
选项
< /a> 属性:可用选项之一是超时
,类型为TimeSpan
:The ManagementObjectSearcher has an
Options
property: one of the available options isTimeout
, of typeTimeSpan
:尝试 co.Timeout = new TimeSpan(0, 0, 30);
Try
co.Timeout = new TimeSpan(0, 0, 30);