我试图ping一个使用Ping类的服务器,但在10次之后,该方法返回true,我总是收到错误(这意味着服务器关闭了[?],它不是)下面是方法:使用C#ping服务器使用C#
public bool IsConnectedToInternet()
{
Ping p = new Ping();
try
{
PingReply reply = p.Send("www.uic.co.il", 1000);
if (reply.Status == IPStatus.Success)
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
return false;
}
private void start_Click(object sender, EventArgs e)
{
for (; ;)
{
Console.WriteLine(IsConnectedToInternet);
}
}
为什么我在一段时间后一直变得虚假? 谢谢。快
for (; ;)
{
Console.WriteLine(IsConnectedToInternet);
}
将循环的请求后,可以发送请求:
您的异常处理是最糟糕的我”在很长一段时间里已经见过。不要理解这个例外:http://blog.gauffin.org/2010/11/do-not-catch-that-exception/。而'IsConnectedToInternet'应该是一个方法'CheckConnection()'而不是属性。 – jgauffin 2012-03-09 13:05:55
请考虑将其移至方法。预计房产没有成本。 – 2012-03-09 13:06:33
在你的'Catch'子句中,尝试'Catch(Exception ex){Console.WriteLine(Ex.Message.toString())}'看看会发生什么。 – 2012-03-09 13:08:01