1 private static async Task<bool> IsPingSuccess()
2 {
3 try
4 {
5 using (Ping myPing = new Ping())
6 {
7 var result = await myPing.SendPingAsync("google.com", 3000 /*3 secs timeout*/, new byte[32], new PingOptions(64, true));
8 return result.Status == IPStatus.Success;
9 }
10 }
11 catch
12 {
13 // ignored
14 }
15 return false;
16 }
ping获取网络状态的耗时,一般在几十ms,延时性偏高。网络不稳定时,执行1s也是可能的,不建议使用