技术开发 频道

利用BackgroundWorker检测网络连接

  连接成功后将显示一个远程网页,失败时将显示一个自定义Html文件。如下图:





  对WebBrowser的DocumentCompleted事件中添加相应代码以对网页装载完成做出响应:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            toolStripStatusLabel1.Text
= "完成";
        }

  最后在Button的单击事件中添加开始测试连接代码:

private void button1_Click(object sender, EventArgs e)
        {
            internetConnection1.ReliableURL
= "http://www.google.cn"; //要检测的网络连接
            internetConnection1.Active = true;
            toolStripStatusLabel1.Image
= Properties.Resources.Searching;
            toolStripStatusLabel1.Text
= "正在尝试连接远程服务器....";
        }

  至此,一个自定义Internet连接组件就创建完毕。该程序在Windows SP3 + Visual Studio 2008 SP1 环境下编译调试成功。
 

0
相关文章