I have a Dataflow pipeline consisting of several blocks.
When elements are flowing through my processing pipeline, I want to group them by field A
. To do this I have a BatchBlock
with high BoundedCapacity
. In it I store my elements until I decide that they should be released. So I invoke TriggerBatch()
method.
private void Forward(TStronglyTyped data)
{
if (ShouldCreateNewGroup(data))
{
GroupingBlock.TriggerBatch();
}
GroupingBlock.SendAsync(data).Wait(SendTimeout);
}
This is how it looks. The problem is, that the batch produced, sometimes contains the next posted element, which shouldn't be there.
To illustrate:
BatchBlock.InputQueue = {A,A,A}
NextElement = B //we should trigger a Batch!
BatchBlock.TriggerBatch()
BatchBlock.SendAsync(B);
In this point I expect my batch to be {A,A,A}
, but it is {A,A,A,B}
Like TriggerBatch()
was asynchronous, and SendAsync
was in fact executed before the batch was actually made.
How can I solve this?
I obviously don't want to put Task.Wait(x)
in there (I tried, and it works, but then performance is poor, of course).