前台:
一个按钮
后台:
private void button3_Click(object sender, EventArgs e)
{
//这样写只是让线程池启动了一个线程,没有利用多个线程来操作
WaitCallback wait = new WaitCallback(DownLoadThreadPool);
ThreadPool.QueueUserWorkItem(wait);
}
private void DownLoadThreadPool(object ojb)
{
for (int i = 30000; i < 30020; i++)
{
try
{
using (WebClient client=new WebClient())
{
client.DownloadFile(@"http://job.cnblogs.com/offer/" + i + "/", @"d:move" + i + ".html"); // 第一个是网上想要下载的地址,第二个是本地机的地址
//使用匿名委托的简写方式
this.Invoke(new Action(delegate()
{
this.textBox1.AppendText("第" + i + "个帖子已经下载完成
");
}));
}
}
catch (Exception ex)
{ }
}
Action action = new Action(Msg1);
this.Invoke(action);
}
private void Msg1()
{
MessageBox.Show("下载完成");
}