Problem with c # stream

I am trying to simplify the task as follows:

  • I have about 100 files that I would like to read and then process the data.
  • Why do I maintain an array of file names and locations
  • I create streams to read files.

Now my problem is that I would like to make sure that only 5 threads appear at a time, since starting 100+ threads is not a good idea at all.

So, please tell me which approach should I use to ensure that only 5 threads work on time, and as soon as one of them is completed, a new one can be launched.

Thanks everyone

+5
source share
6 answers

/Rx ( .NET 4.0, 3.5):

        var options = new ParallelOptions();
        options.MaxDegreeOfParallelism = 5;

        Parallel.ForEach(GetListOFiles(), options, (file) =>
        {
             DoStuffWithFile(file);
        });

, 5 , , .

+4

5 . ParameterizedThreadStart.

, I/O, .

+2

, , - . , this .

+2

:

. ( ThreadPool), . , .

.

, . ( , ). , , .

, , ThreadPool. / .

+2
source
+1
source

All Articles