C# threading problem
c#, multithreading
Solution
I vote for the task parallel library / Rx (included in .NET 4.0, but downloadable for 3.5):
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;
Parallel.ForEach(GetListOFiles(), options, (file) =>
{
DoStuffWithFile(file);
});
Note that this will use up to 5 threads, but I've seen it use less.
Problem
i am trying to simplify problem as follows, - i have around 100+ files that i would like to read and then process the data - For which i maintain array of file names and location - I spawn threads to do the job of reading files. Now my problem is i would like to make sure that only 5 threads are spawn at a time as starting 100 + threads is not good idea at all. So please tell me what approach i should use to ensure that the only 5 threads are working at time and as soon as even one of them is done new one can be started. Thanks all,