winform线程下载网页信息

界面非常简单,有一个下载按钮,一个名为资源库的按钮,还有一个backgroundWorker1控件(ps:目的当点击下载按钮的时候不影响点击资源库按钮,意为多线程),

backgroundWorker1的DoWork事件中的代码:

for (int i = 30320; i < 30340; i++)
            {
                try
                {
                    WebClient client = new WebClient();
                    client.DownloadFile(@"http://job.cnblogs.com/offer/" + i + "/", @"D: ew" + i + ".html");
                }
                catch (Exception ex)
                {
                    
                   
                }
            }


            Action action = new Action(Msg);
            this.Invoke(action);

Msg方法:

 private void Msg()
        {
            MessageBox.Show("下载成功");
        }

下载按钮后台代码:

this.backgroundWorker1.RunWorkerAsync();

资源库后台代码:

MessageBox.Show("精彩大片,应有尽有");

P.S:当点击下载按钮的时候不影响点击资源库按钮。

原文地址:https://www.cnblogs.com/duanlinlin/p/3142911.html