网络状态检查

public sealed class NetworkHelper
    {
        #region 网络状态

        /// 
        /// 监听计时器
        /// 
        private Timer listenTimer;


        private static NetworkHelper instance;

        /// 
        /// 监听间隔
        /// 
        const int LISTEN_TIME_SPAN = 2000;

        //IsNetworkAlive Description
        const int NETWORK_ALIVE_LAN = 1;
        const int NETWORK_ALIVE_WAN = 2;
        const int NETWORK_ALIVE_AOL = 4;

        const int FLAG_ICC_FORCE_CONNECTION = 1;


        private NetworkHelper()
        {

        }

        static NetworkHelper()
        {
            instance = new NetworkHelper();
        }


        public static NetworkHelper GetNetworkHelperInstance()
        {
            return instance;
        }

        /// 
        /// 检查网络是否连通,有延迟
        /// 
        /// 
        /// 
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        /// 
        /// 
        /// 
        /// 连接饿地址
        /// FLAG_ICC_FORCE_CONNECTION
        /// 0
        [DllImport("wininet.dll")]
        private extern static bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);

        /// 
        /// 检查网络是否连通,需要启动服务
        /// 
        /// 
        [DllImport("sensapi.dll")]
        private extern static bool IsNetworkAlive(out int connectionDescription);

        /// 
        /// 检查是否能建立Internet连接,VISTA不可用
        /// 
        /// 
        /// 0
        [DllImport("sensapi.dll")]
        private extern static bool IsDestinationReachable(string dest, IntPtr ptr);


        /// 
        /// 互联网是否可用
        /// 
        /// 
        public bool IsInternetAlive()
        {
            int status;
            //检查网络是否可用
            if (IsNetworkAlive(out status))
            {
                //如果WAN可用,检查能否建立连接
                //if (status == NETWORK_ALIVE_WAN)
                //{
                if (InternetCheckConnection("http://www.baidu.com", FLAG_ICC_FORCE_CONNECTION, 0) ||
                    InternetCheckConnection("http://www.sina.com.cn", FLAG_ICC_FORCE_CONNECTION, 0) ||
                    InternetCheckConnection("http://www.163.com", FLAG_ICC_FORCE_CONNECTION, 0))
                {
                    return true; //如果能建立连接返回TRUE
                }
                else
                    return false;
                //}
                //else
                //    return false;
            }
            return false;
        }

        /// 
        /// 为NetworkStatusChanged事件处理程序提供数据
        /// 
        public class NetworkChangedEventArgs : EventArgs
        {
            public NetworkChangedEventArgs(bool status)
            {
                IsNetworkAlive = status;
            }

            public bool IsNetworkAlive
            {
                get;
                private set;
            }
        }

        /// 
        /// 表示NetworkStatusChanged事件的方法
        /// 
        public delegate void NetworkChangedEventHandler(object sender, NetworkChangedEventArgs e);

        /// 
        /// 网络状态变更时触发的事件
        /// 
        public event NetworkChangedEventHandler NetworkStatusChanged;

        /// 
        /// 网络状态变更时触发的事件
        /// 
        private void OnNetworkStatusChanged(NetworkChangedEventArgs e)
        {
            if (NetworkStatusChanged != null)
                NetworkStatusChanged(this, e);
        }


        /// 
        /// 监听网络状态
        /// 
        public void ListenNetworkStatus(SynchronizationContext context)
        {
            //获得当前网络状态,并通知
          
            bool currentStatus = IsInternetAlive();
            OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus));

            //启动监听网络状态,2秒钟检查一次,当状态变更时触发事件
            listenTimer = new Timer(delegate
                                    {
                                        bool tmpStatus = IsInternetAlive();
                                        if (currentStatus != tmpStatus)
                                        {
                                            currentStatus = tmpStatus;
                                            context.Post(delegate { OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus)); },null);
                                            
                                        }
                                    }
                                    , null
                                    , 0
                                    , LISTEN_TIME_SPAN);

        }


        /// 
        /// 停止监听网络状态
        /// 
        public void CloseListenNetworkStatus()
        {
            listenTimer.Dispose();
        }

你可能感兴趣的:(.Net)