首先我们需要使用winInet.dll中的InternetGetConnectedState方法来检测本地是否连接网络,然后再通过ping的方式来获取网络状况。

然后我们采用Task来开辟一个线程来定时检测网络状况,最后自定义一个委托,然后用事件来简单的通知网络状况。

具体代码如下:

   public class NetWorkHelper
{
/// <summary>
/// 委托
/// </summary>
/// <param name="flag"></param>
/// <param name="msg"></param>
public delegate void NetStatusDelegate(bool flag, string msg);
public event NetStatusDelegate NetStatusEvent;
public void StartCheck()
{
List<string> urlls = new List<string>() { "www.baidu.com", "www.sina.com", "www.cnblogs.com", "www.163.com", "www.csdn.com" };
Task.Run(async () =>
{
await Task.Run(() =>
{
while (true)
{
try
{
CheckServeStatus(urlls);
}
catch (Exception ex)
{
if (NetStatusEvent!=null)
{
NetStatusEvent(false, "网络异常");
}
}
finally
{
System.Threading.Thread.Sleep(3000);
}
}
});
});
}
// <summary>
/// 检测网络连接状态
/// </summary>
/// <param name="urls"></param>
public void CheckServeStatus(List<string> urls)
{
int errCount = 0;//ping时连接失败个数 if (!LocalConnectionStatus())
{
if (NetStatusEvent != null)
{
NetStatusEvent(false, "网络异常--->无连接");
}
}
else if (!MyPing(urls, out errCount))
{
if ((double)errCount / urls.Count >= 0.3)
{
if (NetStatusEvent != null)
{
NetStatusEvent(false, "网络异常--->连接多次无响应");
} }
else
{
if (NetStatusEvent != null)
{
NetStatusEvent(false, "网络异常--->网络不稳定");
} }
}
else
{
if (NetStatusEvent != null)
{
NetStatusEvent(false, "网络正常");
}
}
}
private const int INTERNET_CONNECTION_MODEM = 1;
private const int INTERNET_CONNECTION_LAN = 2; [System.Runtime.InteropServices.DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved); /// <summary>
/// 判断本地的连接状态
/// </summary>
/// <returns></returns>
private static bool LocalConnectionStatus()
{
try
{
System.Int32 dwFlag = new Int32();
if (!InternetGetConnectedState(ref dwFlag, 0))
{
Console.WriteLine("LocalConnectionStatus--未连网!");
return false;
}
else
{
if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用调制解调器上网。");
return true;
}
else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用网卡上网。");
return true;
}
}
}
catch (Exception ex)
{ }
return false;
}
/// <summary>
/// Ping命令检测网络是否畅通
/// </summary>
/// <param name="urls">URL数据</param>
/// <param name="errorCount">ping时连接失败个数</param>
/// <returns></returns>
public static bool MyPing(List<string> urls, out int errorCount)
{
try
{
bool isconn = true;
Ping ping = new Ping();
errorCount = 0;
try
{
PingReply pr;
for (int i = 0; i < urls.Count; i++)
{
pr = ping.Send(urls[i]);
if (pr.Status != IPStatus.Success)
{
isconn = false;
errorCount++;
}
}
}
catch
{
isconn = false;
errorCount = urls.Count;
}
return isconn;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
} }

  

最新文章

  1. 设计模式之工厂模式VS抽象工厂
  2. using namespace std 和 using std::cin
  3. Android 框架练成 教你打造高效的图片加载框架(转)
  4. CocoaPods 抛出[!] Unable to satisfy the following requirements: 错误
  5. 爬虫6:多页面增量Java爬虫-sina主页
  6. 安装Vmware workstation虚拟机(含软件和注册码)
  7. Ubuntu 安装vsftp软件(已测试)
  8. C# 常用的工具类
  9. HDU 1004 - Let the Balloon Rise(map 用法样例)
  10. cuzysdk购物模块 36kr+本期背景图
  11. java伪代码
  12. 精彩源于起点——2018年潍坊市首次青少年Python编程公开课
  13. java--基本数据类型的转换(自动转换)
  14. node note
  15. Hadoop HA方案调研
  16. java中的强,软,弱,虚引用
  17. 【Web】网页字体图标的使用
  18. 深度学习之TensorFlow(一)——基本使用
  19. IOS中的手势详解
  20. EventStore .NET API Client在使用线程池线程同步写入Event导致EventStore连接中断的问题研究

热门文章

  1. 如何搭建属于自己的Web服务器
  2. Linux帮助——重要文件
  3. JDBC连接mysql的url的写法和常见属性
  4. CSS animation 属性
  5. FastJSON使用例子
  6. Python—时间模块(time)和随机模块(random)
  7. Python—内置三大装饰器(@staticmethod、@classmethod、@property)
  8. [Linux]gocron定时任务平台的部署
  9. ts开发环境搭建
  10. MATLAB高斯混合数据的生成