网络连接判断,使用ping测试获取:

 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也是可能的,不建议使用

相关文章:

  • 2022-12-23
  • 2021-10-12
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
猜你喜欢
  • 2021-09-02
  • 2021-10-18
  • 2022-02-14
  • 2021-10-22
  • 2022-02-11
  • 2021-11-12
  • 2022-12-23
相关资源
相似解决方案