用Task控制线程
|
在这样的一些代码中
for (int i = 0; i < length; i++) //each iteration in another task
{
Method2();
}
//Task.WaitAll()
public void Method2()
{
Method3();
}
public void Method3()
{
Method4();
}
public void Method4()
{
process1.Start(); //this process take a lot of time so next iteration/next task should start in this place
}
我想在其他线程中运行每个迭代,但不是同时运行。一个线程应该转到Method4(),运行它,然后等待结束此过程。稍后另一个具有相同行为等的线程。在Task.WaitAll()中,程序应等待所有线程。
这该怎么做? 1个迭代中有2个任务,ContinueWith或sth?
没有找到相关结果
已邀请:
2 个回复
嘘伪
循环替换为
,并在Process代码周围使用简单的
:
但是现在您必须提防TPL创建太多线程。 (在Parallel.For中使用DegreeOfParallelism)。
感秆暴壳