Based on the local network timing detection network conditions Tast

First, we need to use the method winInet.dll in InternetGetConnectedState to detect whether the local network connection, and then to get the network conditions by way of ping.

Then we use the Task to open up a thread to detect the timing of network conditions, and finally a custom delegate, and then use the event to a simple notification network conditions.

Specific code as follows:

   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> 
        /// network connection status detecting 
        /// </ Summary> 
        /// <param name = "URLs"> </ param> 
        public void CheckServeStatus (List <String> URLs) 
        { 
            int errCount = 0; when the number of connection failures of ping // 

            IF (LocalConnectionStatus ()!) 
            { 
                IF (the NetStatusEvent = null!) 
                { 
                    the NetStatusEvent (to false, "network anomalies ---> no connection"); 
                } 
            } 
            the else IF (! MyPing (URLs, errCount OUT)) 
            { 
                IF ((Double) errCount / urls.Count> = 0.3)
                { 
                    IF (the NetStatusEvent! = null)
                    { 
                        The NetStatusEvent (to false, "network anomalies ---> connected repeatedly no response"); 
                    } 
                   
                } 
                the else 
                { 
                    IF (! The NetStatusEvent = null) 
                    { 
                        the NetStatusEvent (to false, "network anomalies ---> network instability"); 
                    } 
                   
                } 
            } 
            the else 
            { 
                IF (! the NetStatusEvent = null) 
                { 
                    the NetStatusEvent (to false, "network normal"); 
                } 
            } 
        }
        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-- non-networked!"); 
                    Return to false; 
                } 
                the else 
                { 
                    IF ((dwFlag & INTERNET_CONNECTION_MODEM) = 0!) 
                    { 
                        Console.WriteLine ( "LocalConnectionStatus-- modem using the Internet."); 
                        Return to true ; 
                    } 
                    the else IF ((dwFlag & INTERNET_CONNECTION_LAN) = 0!) 
                    { 
                        Console.WriteLine ( "LocalConnectionStatus-- use card online."); 
                        return to true; 
                    } 
                } 
            } 
            the catch (Exception EX) 
            { 
                
              
            } 
            return to false; 
        }
        /// <Summary> 
        /// the Ping command to the network is detected smooth 
        /// </ Summary> 
        /// <param name = "URLs"> the URL data </ param> 
        /// <param name = "errorCount"> when the number of failed connection ping </ param> 
        /// <Returns> </ Returns> 
        public static BOOL MyPing (List <String> URLs, int errorCount OUT) 
        { 
            the try 
            { 
                BOOL = isconn to true; 
                the ping = new new ping the ping () ; 
                errorCount = 0; 
                the 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());
            }
        }

    }

  

Guess you like

Origin www.cnblogs.com/hglSV/p/11570216.html