我正在尝试将一些数据推送到BatchBlock,我需要在foreach循环中逐个传递它。我试图搜索一些方法将数组传递给batchBlock,但我没有找到任何类似的方法。
是否有任何方法或解决方法来接受数组,如果不是,不包含此功能的原因是什么。
var modelsDataTable = modelsAndFormulasDataSet.Tables[0];
foreach (DataRow row in modelsDataTable.Rows)
{
modelIdsBatchBlock.Target1.Post<long>(row.Field<long>(AppConstants.ModelsDataTable.ModelId.ToString()));
}
答案 0 :(得分:2)
感谢您澄清问题。基于这种期望的行为:
我有一种情况,
var transformBlock = new TransformBlock<int, List<long>>
,TransformBlock
返回List<long>
,我将其链接到BatchBlock transformBlock.LinkTo(batchBlock)
,而batchBlock
应该在批量化时达到值到3个值而不是15个值。
您正在寻找的是TransformManyBlock
。这是一个在NUnit测试中模拟您想要的行为的示例:
[TestFixture]
public class BlockTester
{
[Test]
public async Task BuildPipeline()
{
var inputBlock = new TransformBlock<int, List<long>>(x => Enumerable.Repeat((long)0, 6).ToList());
var xFormBlock = new TransformManyBlock<List<long>, long>(x => x);
var batchBlock = new BatchBlock<long>(3);
var testBlock = new ActionBlock<long[]>(x => Assert.AreEqual(3, x.Length));
inputBlock.LinkTo(xFormBlock, new DataflowLinkOptions() { PropagateCompletion = true });
xFormBlock.LinkTo(batchBlock, new DataflowLinkOptions() { PropagateCompletion = true });
batchBlock.LinkTo(testBlock, new DataflowLinkOptions() { PropagateCompletion = true });
await inputBlock.SendAsync(1);
await inputBlock.SendAsync(2);
await inputBlock.SendAsync(3);
inputBlock.Complete();
await testBlock.Completion;
}
}