C# 动态地将函数分配给线程

C# 动态地将函数分配给线程,c#,.net,multithreading,.net-4.0,C#,.net,Multithreading,.net 4.0,希望生成一些线程,并在以后的循环中为每个线程分配函数 使用 var threadList = new List<Thread>(); for (int index = 0; index < 3; index++) { threadList.Add(new Thread(() => { })); } var threadList=newlist(); 对于(int-index=0;index{})); } 现在,在一个循环中,您希望将函数分配给线程 for (i

希望生成一些线程,并在以后的循环中为每个线程分配函数

使用

var threadList = new List<Thread>();
for (int index = 0; index < 3; index++)
{
    threadList.Add(new Thread(() => { }));
}
var threadList=newlist();
对于(int-index=0;index<3;index++)
{
添加(新线程(()=>{}));
}
现在,在一个循环中,您希望将函数分配给线程

for (int index = 0; index < names.Length; index++)
{
    string tempName = names[index];
    //check which thread is available
    //assign a function to the first available thread
}
for(int index=0;index
我该怎么做

如果我正在使用线程池:
当所有线程都完成时,如何得到通知?如何停止线程池以执行队列中的任何qction

您不能改用ThreadPool吗

System.Threading.ThreadPool.QueueUserWorkItem(myMethod);

这会将您的方法分配给一个可用的线程,该线程完成后将被释放。

查看其他人如何尝试实现“线程池”,即在本SO问题中,感谢您的输入。。然而,ThreadPool不是有缺点吗?如何在ThreadPool.QueueUserWorkItem中使用参数不是对象类型的方法?如何在所有线程都完成时收到通知?如何停止线程池以执行队列中的任何qction?