监听网络状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Threading;
using System.Runtime.InteropServices;
namespace NetWork
{
    public sealed class NetworkHelper
    {
        #region 网络状态
 
        /// <summary>
        /// 监听计时器
        /// </summary>
        private Timer listenTimer;
 
 
        private static NetworkHelper instance;
 
        /// <summary>
        /// 监听间隔
        /// </summary>
        const int LISTEN_TIME_SPAN = 50000;
 
        //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;
        }
 
        /// <summary>
        /// 检查网络是否连通,有延迟
        /// </summary>
        /// <param name="connectionDescription"></param>
        /// <param name="reservedValue"></param>
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="lpszUrl">连接饿地址</param>
        /// <param name="dwFlags">FLAG_ICC_FORCE_CONNECTION</param>
        /// <param name="dwReserved">0</param>
        [DllImport("wininet.dll")]
        private extern static bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);
 
        /// <summary>
        /// 检查网络是否连通,需要启动服务
        /// </summary>
        /// <param name="connectionDescription"></param>
        [DllImport("sensapi.dll")]
        private extern static bool IsNetworkAlive(out int connectionDescription);
 
        /// <summary>
        /// 检查是否能建立Internet连接,VISTA不可用
        /// </summary>
        /// <param name="dest"></param>
        /// <param name="ptr">0</param>
        [DllImport("sensapi.dll")]
        private extern static bool IsDestinationReachable(string dest, IntPtr ptr);
 
 
        /// <summary>
        /// 互联网是否可用
        /// </summary>
        /// <returns></returns>
        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;
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public int CheckIntertNet()
        {
            int status;
            int connectionDescription;
            //检查网络是否可用
            if (IsNetworkAlive(out status))
            {
                //如果WAN可用,检查能否建立连接
                //if (status == NETWORK_ALIVE_WAN)
                //{
                if (InternetGetConnectedState(out connectionDescription, 0))
                {
                    return 1; //如果能建立连接返回TRUE
                }
                else
                    return 0;
                //}
                //else
                //    return false;
            }
            return 0;
        }
 
        /// <summary>
        /// 为NetworkStatusChanged事件处理程序提供数据
        /// </summary>
        public class NetworkChangedEventArgs : EventArgs
        {
            private bool Isnetworkalive;
            int Isnetworkalive_Int32;
            public NetworkChangedEventArgs(bool status)
            {
                Isnetworkalive = status;
            }
            public NetworkChangedEventArgs(int status)
            {
                Isnetworkalive_Int32 = status;
            }
 
            public bool IsNetworkAlive
            {
                get { return Isnetworkalive; }
                private set { Isnetworkalive = value; }
            }
            public int IsNetworkAlive_Int32
            {
                get { return Isnetworkalive_Int32; }
                private set { Isnetworkalive_Int32 = value; }
            }
        }
 
        /// <summary>
        /// 表示NetworkStatusChanged事件的方法
        /// </summary>
        public delegate void NetworkChangedEventHandler(object sender, NetworkChangedEventArgs e);
 
        /// <summary>
        /// 网络状态变更时触发的事件
        /// </summary>
        public event NetworkChangedEventHandler NetworkStatusChanged;
 
        /// <summary>
        /// 网络状态变更时触发的事件
        /// </summary>
        private void OnNetworkStatusChanged(NetworkChangedEventArgs e)
        {
            if (NetworkStatusChanged != null)
                NetworkStatusChanged(this, e);
        }
 
 
        /// <summary>
        /// 监听网络状态
        /// </summary>
        public void ListenNetworkStatus(SynchronizationContext context)
        {
            //获得当前网络状态,并通知
 
            bool currentStatus = true;
            //bool currentStatus = IsInternetAlive();
 
 
            //OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus));
 
            //启动监听网络状态,LISTEN_TIME_SPAN秒钟检查一次,当状态变更时触发事件
            AutoResetEvent autoEvent = new AutoResetEvent(false);
            listenTimer = new Timer(delegate
                                    {
                                        bool tmpStatus = IsInternetAlive();
                                        if (currentStatus != tmpStatus)
                                        {
                                            currentStatus = tmpStatus;
                                            context.Post(delegate
                                            {
                                                OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus));
                                            }, null);
                                            //OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus));
                                        }
                                    }
                                    , autoEvent
                                    , 0
                                    , LISTEN_TIME_SPAN);
            //autoEvent.WaitOne(5000, false);       
        }
        /// <summary>
        /// 监听网络状态
        /// </summary>
        public void ListenNetworkStatus(SynchronizationContext context, bool? b)
        {
            //获得当前网络状态,并通知
 
            int currentStatus = -1;
            OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus));
 
            //启动监听网络状态,LISTEN_TIME_SPAN秒钟检查一次,当状态变更时触发事件
            AutoResetEvent autoEvent = new AutoResetEvent(false);
            listenTimer = new Timer(delegate
                                    {
                                        int tmpStatus = CheckIntertNet();
                                        if (currentStatus != tmpStatus)
                                        {
                                            currentStatus = tmpStatus;
                                            context.Post(delegate
                                            {
                                                OnNetworkStatusChanged(new NetworkChangedEventArgs(currentStatus));
                                            }, null);
                                        }
                                    }
                                    , autoEvent
                                    , 0
                                    , LISTEN_TIME_SPAN);
            autoEvent.WaitOne(5000, false);
        }
 
 
        /// <summary>
        /// 停止监听网络状态
        /// </summary>
        public void CloseListenNetworkStatus()
        {
            listenTimer.Dispose();
        }
    }
        #endregion
 
}

  

posted @   打倒挨踢者  阅读(180)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示