简体   繁体   English

等待ActionBlock <T> - TPL DataFlow

[英]Awaiting ActionBlock<T> - TPL DataFlow

I am using TPL DataFlow and an ActionBlock to create parallelism. 我正在使用TPL DataFlow和ActionBlock来创建并行性。 The reason for using TPL DataFlow is because it supports asynchronicity, except I can't get it to work. 使用TPL DataFlow的原因是因为它支持异步性,除非我无法使其工作。

var ab = new ActionBlock<Group>(async group =>
{
    try {
        labelStatus.Text = "Getting admins from " + group.Gid;
        await GetAdminsFromGroup(group.Gid);
    }catch (ArgumentOutOfRangeException ex) {
        // Log exception
    }

 }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 10 });

 db.Groups.ToList().ForEach(i => ab.Post(i));

 ab.Complete();

 MessageBox.Show("Complete");

The message box is displaying almost instantly, although the ActionBlocks still run. 虽然ActionBlocks仍在运行,但消息框几乎立即显示。 How can I await until the ActionBlock is complete? 在ActionBlock完成之前我该如何await

ActionBlock<T> exposes a Completion property. ActionBlock<T>公开Completion属性。 That's a Task which completes when the block has finished processing everything. 这是一个Task ,当块完成所有处理后完成。 So you can await that: 所以你可以等待:

ab.Complete();
await ab.Completion;
MessageBox.Show("Complete");

I must admit I haven't used TPL Dataflow myself, but the examples suggest that should be okay. 我必须承认我自己没有使用过TPL Dataflow,但是这些例子表明应该没问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 TPL 数据流操作块没有并行执行? - Why is my TPL Dataflow Actionblock not executing in parallell? TPL.Dataflow - 在ActionBlock中发生无法处理的异常时阻止挂起<T> - TPL.Dataflow - preventing hangs when unhanded exception occurs in ActionBlock<T> 如何将 TPL Dataflow TransformBlock 或 ActionBlock 放在单独的文件中? - How to put a TPL Dataflow TranformBlock or ActionBlock in a separate file? TPL数据流转换块发布到批处理块后跟动作块 - TPL Dataflow Transform block post to batch block followed by actionblock 直接指定动作或使用TPL数据流ActionBlock指定任务生成器有什么区别? - What is the difference between specifying Action directly or Task-Generator with TPL dataflow ActionBlock? TPL数据流。 无法将BatchBlock附加到ActionBlock。 无法从用法中推断出类型实参? - TPL Dataflow. Cannot attach BatchBlock to ActionBlock. Type argument cannot be inferred from usage? TPL 数据流:避免在每次调用其委托时重复运行 using 块(例如写入 StreamWriter)的 ActionBlock - TPL Dataflow: ActionBlock that avoids repeatedly running a using-block (such as for writing to a StreamWriter) on every invocation of its delegate 异步堆栈等效于TPL数据流BufferBlock <T> - Async stack equivalent of TPL Dataflow BufferBlock<T> 取消数据流 ActionBlock 中的特定任务 - Cancelling specific task in a dataflow ActionBlock 自定义ActionBlock <T> - Customizing ActionBlock<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM