0

Want to spawn some threads and assign function to each of them later on in a loop.

Spawning the threads with

var threadList = new List<Thread>();
for (int index = 0; index < 3; index++)
{
    threadList.Add(new Thread(() => { }));
}

Now in a loop want to assign function to the threads

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
}

How do i do it?

If i am using a threadpool: how do i get notified when all the threads are done? how do i stop the threadpool to perform any qction in queue?

priyam
  • 25
  • 1
  • 11
  • Check out how other people tried to implement "thread pool" i.e. in this SO question [Code for a simple thread pool](http://stackoverflow.com/questions/435668/code-for-a-simple-thread-pool-in-c-sharp) – Alexei Levenkov Jan 28 '14 at 05:32

1 Answers1

0

Couldn't you just use ThreadPool instead?

System.Threading.ThreadPool.QueueUserWorkItem(myMethod);

This will assign your methods to an available thread which will be freed up when done.

Erik Karlstrand
  • 1,479
  • 9
  • 22
  • thanks for the input.. however isn't there disadvantages of ThreadPool? how can i use a method in ThreadPool.QueueUserWorkItem where the parameter is not of object type? – priyam Jan 28 '14 at 05:52
  • how do i get notified when all the threads are done? how do i stop the threadpool to perform any qction in queue? – priyam Jan 28 '14 at 06:16