日期:2014-05-17 浏览次数:20988 次
//声明全局变量:所有待处理的网址数组
public static List<string> links = new List<string>();
//按钮事件
private void button1_Click(object sender, EventArgs e)
{
threadOne = new Thread(new ThreadStart(getItemDetail));//两个线程共同做一件事情
threadTwo = new Thread(new ThreadStart(getItemDetail));//两个线程共同做一件事情
threadOne.Name = "线程1";
threadTwo.Name = "线程2";
threadOne.Start();
threadTwo.Start();
}
private void getItemDetail() //处理函数
{
string link = null; //单条网址
while (true)
{
Monitor.Enter(links);//锁定,保持同步
link = (string)links[0];
。。。somecode here// 具体的处理语句,每次处理需要耗时一分钟
links.RemoveAt(0);//删除链接数组List中的元素
if (links.Count == 0)
{
ThreadEnd();//引发完成事件
}
toolStripStatusLabel1.Text = Thread.CurrentThread.Name + "抓取" + link + "成功...";
Monitor.Exit(links