获取电脑的网络连接状态(三)Ping
网络连接判断,使用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也是可能的,不建议使用
作者:唐宋元明清2188
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。