I'm writing a program where I typically start five threads. The threads return in a non-determinate order. Each thread is calling a method which returns a List.
I'm doing this:
var masterList = List<string>(); foreach (var threadParam in threadParams) { var expression = threadParam ; ThreadStart sub = () => MyMethod(expressions); var thread = new Thread(sub) { Name = expression }; listThreads.Add(thread); thread.Start(); } var abort = true; while (abort) //Wait until all threads finish { var count = 0; foreach (var list in listThreads) { if (!list.IsAlive) { count++; } } if (count == listThreads.Count) { abort = false; } } So here is the problem:
Each thread when it terminates returns a list I would like to append the masterList declared earlier.
How would one go about this?
Also I KNOW there must be a better way than below to wait for all threads to finish
var abort = true; while (abort) //Wait until all threads finish { var count = 0; foreach (var list in listThreads) { if (!list.IsAlive) { count++; } } if (count == listThreads.Count) { abort = false; } }