Question

I have a collection of files and I need to process them in parallel. The processing includes uploading each file to an FTP-server as a final step. The number of simultaneous connections on that server is limited, let's say, to 2. I use Parallel.Foreach() like this:

Parallel.ForEach(files, options, ProcessFile);

Do I guarantee by passing to Parallel.ForEach

ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = 2 };

that I won't exceed the number of allowed connections? I've read a lot about how Parallel.ForEach works , but I am still not sure. I consider the case that I am the only one user of FTP-server and every connection is closed properly during ProcessFile.

Était-ce utile?

La solution

Yes, that will ensure you have at most two tasks running at the same time. Assuming that no other process or task is doing any uploads to that server, you should be fine.

I would suggest to use a semaphore inside your foreach loop instead, because then the actual processing can be done with more than two tasks at the same time. This improves performance if the processing is the part that takes most time.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top