本文介绍了如果排队的项目数小于BatchSize,如何在超时后自动调用TriggerBatch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据流CTP(在TPL中)

Using Dataflow CTP (in the TPL)

如果超时后当前排队或推迟的项目数小于BatchSize,是否可以自动调用BatchBlock.TriggerBatch?

Is there a way to call BatchBlock.TriggerBatch automatically if the number of currently queued or postponed items is less than the BatchSize, after a timeout ?

更好的是:每次块收到新项目时,此超时应重置为0.

And better: this timeout should be reset to 0 each time the block receives a new item.

推荐答案

是的,您可以通过将块链接在一起来完成此操作.在这种情况下,您要设置一个TransformBlock,该链接在BatchBlock之前链接".看起来像这样:

Yes, you can accomplish this rather elegantly by chaining together blocks. In this case you want to setup a TransformBlock which you link "before" the BatchBlock. That would look something like this:

Timer triggerBatchTimer = new Timer(() => yourBatchBlock.TriggerBatch());

TransformBlock<T, T> timeoutTransformBlock = new TransformBlock<T, T>((value) =>
{
    triggerBatchTimer.Change(5000, Timeout.Infinite);

    return value; 
});

timeoutTransformBlock.LinkTo(yourBatchBlock);

yourBufferBlock.LinkTo(timeoutTransformBlock);

这篇关于如果排队的项目数小于BatchSize,如何在超时后自动调用TriggerBatch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:57