ThreadPool

static void Main(string[] args)
{
//放进去的顺序不一定和执行的顺序一致!多线程执行中需要注意的一点!
//把任务分配给10个人,执行完成的顺序是不可预测的,不确定的
for (int i = 0; i < 100; i++)
{
Console.WriteLine("准备把{0}号任务放到了QueueUserWorkItem中", i);
//ThreadPool.QueueUserWorkItem(DoJob,i);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoJob), i);
//WaitCallback w1 = new WaitCallback(DoJob);
//WaitCallback w2 = DoJob;//这写法是new WaitCallback(DoJob)的简写
Console.WriteLine("{0}号任务已经放到了QueueUserWorkItem中",i);
//放到线程池中并不代表会立即会执行,线程池会根据线程池中的繁忙情况来安排委托代码的执行。

}
Console.ReadKey();
}

static void DoJob(object state)
{
Console.WriteLine(state);//这句话和Console.WriteLine("{0}号任务已经放到了QueueUserWorkItem中",i);的执行顺序是不确定的。
Console.WriteLine("{0}执行完毕", state);//这两句话的先后顺序是确定的,当然中间可能会被别的线程插入东西
}
namespace 多线程下载帖子
{
public partial class Form1 : Form
{
private DateTime startTime;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 1; i <= 100; i++)
{
using (WebClient webclient = new WebClient())
{
webclient.DownloadString("http://nt.discuz.net/showtopic-"+i+".html");
}
}
stopwatch.Stop();
MessageBox.Show(stopwatch.ElapsedMilliseconds.ToString());//21秒
}
/// <summary>
/// 多线程下载按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//强类型DataSet
T_ThreadsTableAdapter adapter = new T_ThreadsTableAdapter();
//删除数据
adapter.DeleteAll();
//循环插入数据序号,和状态 0未下载,1下载
for (int i = 1; i <= 100; i++)
{
adapter.Insert(i, 0, "");
}
//记时
startTime = DateTime.Now;
//循环下载
for (int i = 1; i <= 100; i++)
{
//将下载信息放入线迟中
//运用委托,调用下载函数 也可以 ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadHtml), i);
ThreadPool.QueueUserWorkItem(DownloadHtml, i);
}
//将更新下载状态函数放入线程池 也可以 ThreadPool.QueueUserWorkItem(new WaitCallback(ScanFinished), i);
ThreadPool.QueueUserWorkItem(ScanFinished);//为什么这不需要参数,而ScanFinished还需要参数?
//多线程的好处,批量代发工资的项目,rupeng域名的获得,站内搜索爬帖子
//以后做项目的时候发现程序运行速度慢,第一个想到的解决方案就是多线程,百试不爽!
}
/// <summary>
/// 下载函数方法
/// </summary>
/// <param name="state"></param>
private void DownloadHtml(object state)
{
T_ThreadsTableAdapter adapter = new T_ThreadsTableAdapter();
int tid = Convert.ToInt32(state);
using (WebClient webclient = new WebClient())
{
string html = webclient.DownloadString("http://nt.discuz.net/showtopic-" + tid + ".html");
adapter.UpdateThread(1, html, tid);//任务完成,更新数据库表中的任务状态为:已经完成
}
}
/// <summary>
/// 更新下载状态函数方法
/// </summary>
/// <param name="state">委托必须要求的参数,当没有参数的情况下,State为null</param>
private void ScanFinished(object state)
{
T_ThreadsTableAdapter adapter = new T_ThreadsTableAdapter();
while (true)//一遍遍的扫描
{
int count = (int)adapter.GetUnfinishedCount();
if (count <= 0)//当任务全部完成的时候count为0,退出扫描
break;
}
//在数据执行完成后,将后台线程方法放到主线程中去显示打印信息
this.BeginInvoke(new Action(delegate()
{
TimeSpan ts = DateTime.Now - startTime;
MessageBox.Show("任务全部完成,时间:" + ts.TotalMilliseconds);//11秒,因为网速的原因,并发下载互相影响下载速度,所以提速不是很明显。
}));

}

}
}



原文地址:https://www.cnblogs.com/happygx/p/2379091.html