【发布时间】:2013-08-14 09:05:31
【问题描述】:
我是线程编程的新手。我必须在 PARALLEL 和后台运行一些任务(以便主 UI 执行线程保持对用户操作的响应)并等待每个任务完成,然后再继续执行。
类似:
foreach(MyTask t in myTasks)
{
t.DoSomethinginBackground(); // There could be n number of task, to save
// processing time I wish to run each of them
// in parallel
}
// Wait till all tasks complete doing something parallel in background
Console.Write("All tasks Completed. Now we can do further processing");
我知道有几种方法可以实现这一点。但我正在寻找在 .Net 4.0 (C#) 中实现的最佳解决方案。
【问题讨论】:
-
你如何实现 MyTask?
-
好吧,MyTask 可以是任何东西。在这里没关系。在我的实际解决方案中,我有一个 URL 数组,我必须为每个 URL 获取 HTML(Web Scrapping)。
标签: c# multithreading parallel-processing