WinForm开发中检测网络是否连接

 1     public partial class FrmMain : Form
 2     {
 3         private readonly Thread CheckWanThread = null;
 4 
 5         private readonly int CheckIntervalTime = 1000;
 6 
 7         private readonly Ping ping = null;
 8 
 9         private String IP = "www.hao123.com";
10 
11         public FrmMain()
12         {
13             InitializeComponent();
14             this.ping = new Ping();
15             this.CheckWanThread = new Thread(new ParameterizedThreadStart(CheckWan));
16             this.CheckWanThread.IsBackground = true;
17             this.CheckWanThread.Start();
18         }
19 
20         private void CheckWan(object data)
21         {
22             String temp = "";
23             while (true)
24             {
25                 if (ping.Send(IP).Status == IPStatus.Success)
26                     temp = "网络:OK";
27 
28                 else
29                     temp = "网络:Error";
30                 this.tsslWan.Text = temp;
31                 Thread.Sleep(CheckIntervalTime);
32             }
33         }
34     }

 

posted @ 2010-02-07 21:50  冯翔  阅读(1322)  评论(1)    收藏  举报