Given
- This code represents some module/service
- The module receives a group of items each time and start process it
- The module should's wait for previous groups to complete to start processing the new one (only CPU limitations)
The output of the code below is: 000000000011111111112222222222
Hot to change the code to make next output: 012012012012012012012012012012
?
var executionDataflowBlockOptions =
new ExecutionDataflowBlockOptions() {
MaxDegreeOfParallelism = 3, EnsureOrdered = false, BoundedCapacity = 30 };
var testData =
Enumerable
.Range(0, 3)
.Select(o => Enumerable.Repeat(o, 10));
var inputBlock =
new TransformManyBlock<IEnumerable<int>, int>(
collectionOfItems => collectionOfItems,
executionDataflowBlockOptions);
var processBlock =
new ActionBlock<int>(
number =>
{
Thread.Sleep(100);
Console.Write(number);
},
executionDataflowBlockOptions );
inputBlock
.LinkTo(processBlock);
foreach (var item in testData)
{
await inputBlock.SendAsync(item);
}
UPDATE (EnsureOrdered)
Finally found this option (thanks JSteward). I used the Microsoft.Tpl.Dataflow (which looks obsolete) instead of System.Threading.Tasks.Dataflow. Not sure why nuget search brings them in a such strange order.
Anyway adding this option changes results. Sometime it outputs 000000000022222222221111111111 (changes order groups), Changing the BoundedCapacity=30 sometime output such results: 210202222222020000000111111111 or 201012222222212111111001000000, which is much better, but still results can be more random