Reputation: 99
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.
Upvotes: 4
Views: 5750
Reputation: 1673
Rather then specifying the count, use the Environment to get so it will be as parallel as possible as capabilities increase.
Parallel.ForEach(
items,
new ParallelOptions {MaxDegreeOfParallelism =
System.Environment.ProcessorCount
},
item => {
// Work with item
}
);
Upvotes: 1
Reputation: 62504
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
Upvotes: 11