获取信息可用的批次 [英] Get batches of messages as available
问题描述
我想实现使用任务并行库以下行为:
在消息到达我想顺序,但在小组处理。因此,第一个消息到达时,它应该被立即处理。如果2的消息进来,而第一个被处理,就应该一组2中进行处理。
我几乎可以得到我想要使用<$ C $什么C> BatchBlock 链接到 ActionBlock
变种batchBlock =新BatchBlock&所述; INT>(100);
变种actionBlock =新ActionBlock< INT []>(名单=>
{
//做的工作
//现在触发
batchBlock.TriggerBatch();
});
batchBlock.LinkTo(actionBlock);
与上面的代码的问题是,如果一个项目到达后的 TriggerBatch ()
通话则需要等待批填补。如果我每个岗位,而不是那么 ActionBlock
总是收到一个消息。
您也可以使用定时器;这将触发每10秒批
I am trying to achieve the following behaviour using the Task Parallel Library:
As messages arrive I would like to process them sequentially but in groups. So when the first message arrives it should be processed immediately. If 2 messages come in while the first is being processed then they should be processed in a group of 2.
I can almost get what I want using a BatchBlock
linked to an ActionBlock
var batchBlock = new BatchBlock<int>(100);
var actionBlock = new ActionBlock<int[]>(list =>
{
// do work
// now trigger
batchBlock.TriggerBatch();
});
batchBlock.LinkTo(actionBlock);
The problem with the code above is that if an item arrives after the TriggerBatch()
call then it needs to wait for the batch to fill up. If I trigger batch after each post instead then the ActionBlock
always receives single messages.
You can also use Timer; which will Trigger Batch on every 10 seconds
这篇关于获取信息可用的批次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!