[英]How to check if internet connection was lost WP7
How can I check if internet connection was lost in WP7.I'm developing social app and if internet connection was lost I have FileNotFoundException.I can't use try/catch because its doesn't prevent my app from closing with exception.Also if (NetworkInterface.GetIsNetworkAvailable())
works only from time to time and not always shows the truth.如何检查 WP7 中的互联网连接是否丢失。我正在开发社交应用程序,如果互联网连接丢失我有 FileNotFoundException。我不能使用 try/catch,因为它不会阻止我的应用程序异常关闭。还有if (NetworkInterface.GetIsNetworkAvailable())
仅不时起作用,并不总是显示真相。
The only reliable way I have found is to ping something reliable.我发现唯一可靠的方法是 ping 可靠的东西。 My customer set up a "ping" call on the server that I need to access, which is the ultimate test.我的客户在我需要访问的服务器上设置了“ping”调用,这是终极测试。 If I can reach that call, I can (or shoule be able to) reach any of them.如果我能接到那个电话,我就可以(或应该能够)接到他们中的任何一个。
bool isAvailable = NetworkInterface.GetIsNetworkAvailable();
if (isAvailable == false)
{
//Logic here
}
else
{
//Code
}
It works all the time for me...它一直对我有用...
NetworkInterface.GetIsNetworkAvailable()
doesnt seem to work correctly all the time. NetworkInterface.GetIsNetworkAvailable()
似乎并不总是正常工作。 Its better to use WebClient
object to download some URL which you are sure is available always.最好使用WebClient
object 下载一些您确定始终可用的 URL。
@Francesco: I think you are right, NetworkInterface.GetIsNetworkAvailable()
works fine on phone, but its unreliable on emulator. @Francesco:我认为您是对的, NetworkInterface.GetIsNetworkAvailable()
在手机上运行良好,但在模拟器上不可靠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.