3
Reply

Thread.Join()

Norm Pence

Norm Pence

Sep 2 2009 12:25 AM
3.9k

So I need to iterate through 8000 records and thread each record out using multiple threads which is not an issue. Now I need to perform an action after the last thread is processed. How do I expose a thread outside a simple if-then-else statement and "While statement" in order to join the threads after reading the entire recordset. I don't want to perform a join on the first 7992 threads. Simplified code follows with locking and such removed:
While (OracleReader.Read)
{
   if (dc ==3)
   {
   ThreadStart starter = delegate { net_adapter(msg_type, msg_id, sb_api.ToString(), dc, my_trailer, my_lpn); };
   Thread myThread = new Thread(starter);
   myThread.Start();
   }
}
myThread.Join <-I want to join any outstanding threads here (there may be 8 or less) and I receive "The name myThread does not exist in the current context when trying to compile. Yes I can join them earlier but I don't care about the first 7992 threads and don't need that overhead. Threadpool for these threads is 8. I don't necessarily need the error message explained and am more looking for a solution to the problem.

Answers (3)