我有以下代码...
Ping ping = new Ping();
PingReply replay = ping.Send(ipAddress, timeOut* 1000);我在IP仍然关闭的情况下运行该函数,并将timeOut设置为大约5分钟。我还打开了一个CMD窗口,我在其中写入了ping ipaddress -t。
当计算机仍然关闭时,我在CMD窗口和我的程序中都得到了超时。
大约一分钟后
我用IP地址打开计算机。
CMD窗口立即检测到机器,并在ping响应地址中获取地址,但我必须等待整个超时到期才能从.NET函数获取地址。
为什么它不像CMD中那样在ping正常时立即回复?
发布于 2013-11-26 22:45:14
因此,我决定为此编写自己的ping()。
private PingReply ping(int tout,bool waitForSuccess)
{
PingReply replay = null;
Ping ping = new Ping();
Stopwatch stopWatch=new Stopwatch();
stopWatch.Start();
long timeOut=tout*1000;
while (stopWatch.ElapsedMilliseconds < timeOut)
{
replay = ping.Send(m_device.IPAddress,2000);
if((replay.Status == IPStatus.Success) == waitForSuccess)
break;
System.Threading.Thread.Sleep(2000);
}
return replay;
}https://stackoverflow.com/questions/20218585
复制相似问题