I have an ordered list like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
. I am passing it to a Parallel.ForEach
statement. Can I somehow achieve the following ordering of execution of buckets like: process first 3 items [1, 2, 3]
where ordering in bucket itself is not mandatory and can be [2, 1, 3]
for instance. Then process next 3 items [4, 5, 6]
, etc?
I'm not sure that you can do this directly. but I would suggest you to divide the input list in to smaller lists and then you can process each sublist with Parallel.Foreach
.
List<int> fruits = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<List<int>> ls = new List<List<int>>();
for (int i = 0; i < fruits.Count; i += 3)
{
ls.Add(fruits.GetRange(i, Math.Min(3, fruits.Count - i)));
}
foreach (List<int> group in ls)
{
Parallel.ForEach(group, fruit =>
{
});
}
3 is the length of small list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With