c#上线程数组末端的等待是否正确?

c#上线程数组末端的等待是否正确?,c#,.net-3.5,C#,.net 3.5,我需要使用clear thread类(不是Task,也没有async/await)。如何正确等待列表中所有任务的结束 我想要正确的方法WaitAll() 例如: public class TaskManager { private readonly List<Thread> _threads = new List<Thread>(); public void AddTask([NotNull] Action<int> action, int

我需要使用clear thread类(不是Task,也没有async/await)。如何正确等待列表中所有任务的结束

我想要正确的方法WaitAll()

例如:

public class TaskManager
{
    private readonly List<Thread> _threads = new List<Thread>();

    public void AddTask([NotNull] Action<int> action, int i)
    {
        var thread = new Thread(() =>
        {
            action(i);
        });
        _threads.Add(thread);
        thread.Start();
    }

    public void WaitAll()
    {
        while (_threads.Any(x => x.ThreadState != ThreadState.Stopped))
        {
        }
    }
}
公共类任务管理器
{
私有只读列表_threads=new List();
public void AddTask([NotNull]Action Action,int i)
{
变量线程=新线程(()=>
{
行动(一);
});
_线程。添加(线程);
thread.Start();
}
公共服务
{
while(_threads.Any(x=>x.ThreadState!=ThreadState.Stopped))
{
}
}
}

我质疑是否需要“裸线程”,但如果你确定这一点,那么在while循环中等待就是在浪费CPU时间。线程只有Join()方法可用于:

public void WaitAll()
{
   foreach(var thread in _threads)    
     thread.Join();
}

为什么需要“清除线程类(而不是任务…?”?这立刻让它看起来像一个X/Y问题。
public void WaitAll(){foreach(var t in_threads)t.Join()}
这是否回答了您的问题?我需要它的项目在FW3.5在3.5你至少有线程池,但这将使你的管理非常不同。也许甚至没有必要。