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