【发布时间】:2011-08-21 20:41:09
【问题描述】:
我已经搜索了一上午,但似乎找不到这个问题的答案。
我有一个线程数组,每个线程都在工作,然后我将循环遍历加入每个线程的 id,然后启动新线程。检测线程何时完成的最佳方法是什么,以便我可以在不等待每个线程完成的情况下触发新线程?
编辑添加代码 sn-p 也许这会有所帮助
if (threadCount > maxItems)
{
threadCount = maxItems;
}
threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
{
threads[i] = new Thread(delegate() { this.StartThread(); });
threads[i].Start();
}
while (loopCounter < threadCount)
{
if (loopCounter == (threadCount - 1))
{
loopCounter = 0;
}
if (threads[loopCounter].ThreadState == ThreadState.Stopped)
{
threads[loopCounter] = new Thread(delegate() { this.StartThread(); });
threads[loopCounter].Start();
}
}
【问题讨论】:
-
您使用的是 .NET 4 吗?任务并行库通过延续使这相对容易......
标签: c# multithreading