Search code examples
c#.netparallel-processing.net-4.5

C#: Basic approach to processing a list in parallel?


I'd like to do something that I assume is fairly basic, but I haven't found a fairly basic example yet. Here's what I want to do:

Given a list of objects, I want to iterate over each element in the list and call a method on it that's fairly resource intensive. I want to split this out over as many cores as are available to the running application, so that if it's running on an 8-core device, it's processing up to 8 elements at a time. All of the calculations are completely independent from each other, and I expect that as one element is done being processed, the next one in the list will get kicked off.

I'm not sure how many items will be in the list at any given time, and I don't know how many cores will be available. Maybe one one core at times.

Thanks.


Solution

  • Parallel.ForEach(items, currentItem =>
    {
         // TODO: process a current item
         Debug.WriteLine("[{0}] {1} {2}", 
                        Thread.Current.ManagedThreadId , 
                        DateTime.Now,
                        currentItem);
    };
    

    items variable should be an instance of a type which implements IEnumerable or IEnumerable<T>.

    Also you can specify ParallelOptions and MaxDegreeOfParalellism to control a number of threads which can be used by a ThreadPool while distributing a work between threads.

    var options = new ParallelOptions
          {
             MaxDegreeOfParallelism = 2
          };
    
    Parallel.ForEach(items, options, currentItem =>
    {
         // TODO
    });
    

    Also see MSDN: How to: Write a Simple Parallel.ForEach Loop