0

Following this, could anyone post a barebone solution for the following task, targeting .NET 4.5 (and WPF UI on a bigger scale)?

Basically I'm looking for a functional analog of any torrent application implemented on .NET 4.5 and c#.

The task:

I have IEnumerable<IProcessable>, containing 1000 instances of IProcessable, IProcessable has Process(int argument) method, taking from 1 to 10 seconds to execute. I want to loop through the collection and process each instance of IProcessable, limiting the number of concurrently processed instances to N (1..10), number of max concurrent instances should be easily adjustable. Ideally I'd each IProcessable to report about the progress of Process completion, here's a prototype of Process (it probably needs to be converted to something rather than void in order to enable progress reporting):

void Process(int e)
{
   int progress = 0;
   ...Sleep for 100ms;
   int progress = 30;
   ...Sleep for 100ms;
   int progress = 50;
   ...
}
Community
  • 1
  • 1
user1514042
  • 1,899
  • 7
  • 31
  • 57
  • @Tudor the second one, however I don't see much difference as new threads only get created when IProcessable is getting processed, am I missing something? – user1514042 Sep 11 '12 at 10:27

1 Answers1

3

If all you want is to limit the number processed instances at a time, it seems like Parallel.ForEach() would be a good solution. When you use it, you can specify MaxDegreeOfParallelism, which does that.

svick
  • 236,525
  • 50
  • 385
  • 514
  • "Number of max concurrent instances should be easily adjustable". If you mean that it should be adjustable **during** processing, you should not use `Parallel.ForEach()` and instead use a multithreaded producer/consumer queue. – Rotem Sep 11 '12 at 11:16