读书人

判断所有子线程执行完教小弟我上多

发布时间: 2013-03-04 17:22:12 作者: rapoo

判断所有子线程执行完,教我下,谢谢
本帖最后由 skynq 于 2013-02-25 11:19:20 编辑 主函数里面

static void Main(string[] args)
{
//我开了10个线程


for (int i = 0; i < 10; i++)
{
Thread th = new Thread(() =>
{
for (int j = 0; j < htmlUrl.Count; j++)
{
getCount(htmlUrl[j]);
htmlUrl.Remove(htmlUrl[j]); //清空

}
});

th.Start();
}

}

当然主函数里面还有其他调用的方法。我怎么判断这些子线程全部结束在执行我其他的方法。谢谢。
[解决办法]
试试fastCSharp,供参考
            using (fastCSharp.threading.task task = new fastCSharp.threading.task(10, true))
{
for (int i = 0; i < 10; i++)
{
task.Add(() =>
{
//do something
});
}
}
Console.WriteLine("线程执行完毕");


[解决办法]
static void Main()
{
PExecuteMethod();
Console.ReadLine();
}
private static void PExecuteMethod()
{
Parallel.For(0, 10, (i) =>
{
//do your job here
DealWithHtmlUrl(i);
});
Console.WriteLine("任务执行完毕.");

TodoTheLastMethod();
}

private static void TodoTheLastMethod()
{
//The Last Method todo here
Console.WriteLine("The Last Method todo here");
}

private static void DealWithHtmlUrl(int i)
{
//throw new NotImplementedException();

//todo: 针对第i个url的处理
Console.WriteLine("针对第{0}个url的处理", i);
}

读书人网 >asp.net

热点推荐