TPL Dataflow库的几个扩展函数

TPL Dataflow是微软面向高并发应用而推出的新程序库。借助于异步消息传递与管道,它可以提供比线程池更好的控制。本身TPL库在DataflowBlock类中提供了不少扩展函数,用起来还是非常方便的,但感觉还是不够全(当然,MS没必要设计大而全的接口),前段时间写个小程序的时候用到了它,当时顺便写了几个扩展函数,这里记录一下,如果后续有扩展再继续补充。

static class DataFlowExtension     {         /// <summary>         ///同步发送所有数据至TargetBlock         /// </summary>         public static void PostAll<T>(this ITargetBlock<T> target, IEnumerable<T> source)         {             var isSuccess = source.All(i => target.Post(i));             if (!isSuccess)             {                 throw new InvalidOperationException();             }             target.Complete();         }
        /// <summary>         ///异步发送所有数据至TargetBlock         /// </summary>         public static async Task PostAllAsync<T>(this ITargetBlock<T> target, IEnumerable<T> source)         {             foreach (var item in source)             {                 await target.SendAsync(item);             }             target.Complete();         }                  /// <summary>         ///同步从数据源中获取所有数据         /// </summary>         public static IReadOnlyList<T> ReceiveAll<T>(this IReceivableSourceBlock<T> source)         {             IList<T> output;             if (!source.TryReceiveAll(out output))             {                 throw new InvalidOperationException();             }
            return output as IReadOnlyList<T>;         }
        /// <summary>         ///异步从数据源中获取所有数据         /// </summary>         public static async Task<IReadOnlyList<T>> ReceiveAllAsync<T>(this ISourceBlock<T> source)         {             List<T> output = new List<T>();             while (await source.OutputAvailableAsync())             {                 output.Add(source.Receive());             }             return output;         }     }

这几个扩展函数本身是对DataflowBlock类中的函数二次封装,没有太多的功能,基本上每个函数都只有几行,主要为了使用更加方便罢了,由于实现简单,扩充它也是非常方便的。

最新文章

  1. ASP.NET_各个币种之间的汇率转换(实时)使用Yahoo汇率。
  2. Grumpy: Go 上运行 Python!
  3. Theano2.1.7-基础知识之设置的配置和编译模式
  4. addListener添加事件监听器,第三个参数useCapture (Boolean) 的作用
  5. 【bz2002】弹飞绵羊
  6. 第二篇、C_递归算法
  7. Leetcode:best_time_to_buy_and_sell_stock_II题解
  8. ZeroClipBoard 复制粘贴插件
  9. 试水mongodb er
  10. 添加保存less报错
  11. log4j日志输出性能优化-缓存、异步
  12. 201421123042 《Java程序设计》第14周学习总结
  13. java游戏开发杂谈 - 画布和画笔
  14. VulDeePecker:基于深度学习的脆弱性检测系统
  15. Windows -- cmd命令: ipconfig 和 nbtstat
  16. kubernetes系列05—kubectl应用快速入门
  17. Oracle 存储过程笔记.
  18. in和hasOwnProperty的区别
  19. NOIP2018提高组初赛知识点
  20. 20190402Linux高级命令进阶(week1_day2

热门文章

  1. Python菜鸟之路:Django CSRF跨站请求伪造
  2. xenserver 模板导出导入
  3. echarts系列之动态加载数据
  4. Mysql常用优化方案
  5. Python线程包装器
  6. JavaWeb—监听器Listener
  7. PHP用星号隐藏部份用户名、身份证、IP、手机号、邮箱等实例
  8. Linux sh远程连接失败 sshd.service启动失败
  9. mybatis使用注意的细节
  10. springboot中Controller没有被扫描