I have a simple for loop in c# that traverses all elements in an array, and invokes a method ProcessArrayIndex() each time. The result is stored in another array, at the same index.
Expand|Select|Wrap|Line Numbers
- int[] processedArray;
- int[] myArray;
- for (int k = 0; k < myArray.Length; k++)
- {
- myArray[k] = ProcessArrayIndex(k);
- }
I would like to have (for example) 10 threads working in parallel. Each loop iteration grabs one of the free threads for it to execute. If all threads are busy, the loop will wait until a thread becomes available. I tried messing something up with ThreadPool but I didn't manage to get what i want.
Basically I would need:
-> A way to have multiple loop iterations running concurrently (with a limited maximum number of threads)
-> A way to make sure that all threads have finished executing before continuing with the rest of my code!
Any ideas &/or sample code would be very much appreciated!
thanks!