【发布时间】:2015-06-13 10:32:51
【问题描述】:
我需要一种最快的方法来检查远程计算机是否关闭。我尝试了一些方法,但它们都没有给我想要的时间速度(它应该小于 0.5 秒,这很关键,因为发生在上下文菜单弹出窗口中)
//using System.Net
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
//if error occurs - remote computer is off
//using System.Net.NetworkInformation
Ping ping = new Ping();
PingReply pingReply = ping.Send(hostIp);
if (pingReply.Status != IPStatus.Success)
{
//Remote computer is off
}
//using System.Management
ManagementScope scope = new ManagementScope(@"\\" + hostName + @"\root\cimv2", null);
scope.Connect();
//if error occurs - remote computer is off
据我所知,最快的方法是使用 Dns.GetHostEntry(hostName) - 但仍然需要大约 3 秒才能引发异常。 有什么想法吗?
【问题讨论】:
-
您要检查的计算机列表是否有限?
-
是的,每次只有一台特定的计算机。
-
除非您能保证远程计算机在线时能在 0.5 秒内响应,否则您的要求是不可能的,而您无法保证。
标签: c# network-programming remote-access