日期:2014-05-17 浏览次数:20512 次
BackgroundWorker bw = null;
public Form3()
{
InitializeComponent();
bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i < 100; i++)
{
if (bw.CancellationPending)
{
e.Cancel = true;
break;
}
this.Invoke((MethodInvoker)delegate
{
this.richTextBox1.Text += Environment.NewLine + String.Format("第{0}次测试是否联网:{1}", i.ToString(), IsWWW("http://www.baidu.com"));
Thread.Sleep(2000);
});
}
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.richTextBox1.Text += "执行完毕";
}
private void button1_Click(object sender, EventArgs e)
{
this.button1.Enabled = false;
bw.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)