producer把消息发送到消息块,consumer从块读取消息。

安装:

  1. Install-Package Microsoft.Tpl.Dataflow
  2.  
  3. using System.Threading.Tasks.Dataflow;

 

解释:

Produce方法随机生成字节,并Post到ITargetBlock对象;

Consumer方法从ISourceBlock对象读取字节;

可以使用BufferBlock来同时扮演源和目标对象。

Post():同步发送消息。

Complete():表明当前块(source block)已经没有数据更多的数据了。

Consumer方法使用await和async操作符异步地计算总的字节数。

OutputAvailableAsync():从source block收到一个通知(接收到Complete的通知),表明没有更多的数据可用。

 

  1. public
    static
    class DataflowProducerConsumer
  2. {
  3.     // Demonstrates the production end of the producer and consumer pattern.
  4.     static
    void Produce(ITargetBlock<byte[]> target)
  5.     {
  6.         Random rand = new Random();
  7.  
  8.         // In a loop, fill a buffer with random data and
  9.         // post the buffer to the target block.
  10.         for (int i = 0; i < 100; i++)
  11.         {
  12.             // Create an array to hold random byte data.
  13.             byte[] buffer = new
    byte[1024];
  14.  
  15.             // Fill the buffer with random bytes.
  16.             rand.NextBytes(buffer);
  17.  
  18.             // Post the result to the message block.
  19.             target.Post(buffer);
  20.         }
  21.  
  22.         // Set the target to the completed state to signal to the consumer
  23.         // that no more data will be available.
  24.         target.Complete();
  25.     }
  26.  
  27.     // Demonstrates the consumption end of the producer and consumer pattern.
  28.     static async Task<int> ConsumeAsync(ISourceBlock<byte[]> source)
  29.     {
  30.         // Initialize a counter to track the number of bytes that are processed.
  31.         int bytesProcessed = 0;
  32.  
  33.         // Read from the source buffer until the source buffer has no
  34.         // available output data.
  35.         while (await source.OutputAvailableAsync())
  36.         {
  37.             byte[] data = source.Receive();
  38.  
  39.             // Increment the count of bytes received.
  40.             bytesProcessed += data.Length;
  41.         }
  42.  
  43.         return bytesProcessed;
  44.     }
  45.  
  46.     static
    void Run(string[] args)
  47.     {
  48.         // Create a BufferBlock<byte[]> object. This object serves as the
  49.         // target block for the producer and the source block for the consumer.
  50.         var buffer = new BufferBlock<byte[]>();
  51.  
  52.         // Start the consumer. The Consume method runs asynchronously.
  53.         var consumer = ConsumeAsync(buffer);
  54.  
  55.         // Post source data to the dataflow block.
  56.         Produce(buffer);
  57.  
  58.         // Wait for the consumer to process all data.
  59.         consumer.Wait();
  60.  
  61.         // Print the count of bytes processed to the console.
  62.         Console.WriteLine("Processed {0} bytes.", consumer.Result);
  63.     }
  64. }

 

参考:https://msdn.microsoft.com/en-us/library/hh228601(v=vs.110).aspx

How to: 使用 数据流 实现生产者-消费者模式的更多相关文章

  1. java多线程 生产者消费者模式

    package de.bvb; /** * 生产者消费者模式 * 通过 wait() 和 notify() 通信方法实现 * */ public class Test1 { public static ...

  2. LabVIEW之生产者/消费者模式--队列操作 彭会锋

    LabVIEW之生产者/消费者模式--队列操作 彭会锋 本文章主要是对学习LabVIEW之生产者/消费者模式的学习笔记,其中涉及到同步控制技术-队列.事件.状态机.生产者-消费者模式,这几种技术在在本 ...

  3. 转:Task任务调度实现生产者消费者模式 (个人理解后文)

    纯属个人愚见.欢迎加入反驳(PiDou). 1.前文大致就是,利用Queue配置的一个TaskFactory任务调度器.实现生产者消费者模式的例子..首先我就试了 第一种 FIFO(先进先出)的配置. ...

  4. Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  5. MVC异常日志生产者消费者模式记录(异常过滤器)

    生产者消费者模式 定义自己的异常过滤器并注册 namespace Eco.Web.App.Models { public class MyExceptionAttribute : HandleErro ...

  6. 转:Task任务调度实现生产者消费者模式

    我们经常会遇到生产者消费者模式,比如前端各种UI操作事件触发后台逻辑等.在这种典型的应用场景中,我们可能会有4个业务处理逻辑(下文以P代表生产者,C代表消费者): 1. FIFO(先进先出)      ...

  7. .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法

    1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...

  8. 使用BlockingQueue的生产者消费者模式

    BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.使用场景. 首先它是一个队列,而一个队 ...

  9. Java设计模式—生产者消费者模式(阻塞队列实现)

    生产者消费者模式是并发.多线程编程中经典的设计模式,生产者和消费者通过分离的执行工作解耦,简化了开发模式,生产者和消费者可以以不同的速度生产和消费数据.这篇文章我们来看看什么是生产者消费者模式,这个问 ...

随机推荐

  1. 史上最全web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义预览 <web-app> <!--定义了WEB应用的名字--> <display-name></display-na ...

  2. September 25th 2016 Week 40th Sunday

    Everything is good in its season. 万物逢时皆美好. Don't lose hope. Remeber that even a dog has its day. Onc ...

  3. java链式编程设计

    一般情况下,对一个类的实例和操作,是采用这种方法进行的: Channel channel = new Channel(); channel.queueDeclare(QUEUE_NAME, true, ...

  4. Java -- 子类使用super调用父类的方法A,A 调用了方法B,子类也override方法B,那么super.A()最终调用到了子类的B方法

    public class SuperClass{ public void printA(){ System.out.print("SuperClass-printA"); prin ...

  5. Mybatis 字符绑定

    http://blog.csdn.net/softwarehe/article/details/8889206

  6. C#关键字ref和out

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Di ...

  7. Extjs ComboBox 动态选中第一项

    有时候我们希望通过Store加载过来的数据,ComboBoxItem能够选择第一条数据作为默认数据,我们可以这么操作: var storeinfo = Ext.create('Ext.data.Sto ...

  8. Web框架本质

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf- ...

  9. 【JAVA基本数据类型包装类】

    一.概述 JAVA中一共有8种数据类型,分别是byte short int long boolean float double  char,与此相对应的,有8个类与它们分别对应: byte Byte ...

  10. 排序练习【sdut 1582】【堆排序】

    排序 Time Limit: 1000ms   Memory limit: 32678K  有疑问?点这里^_^ 题目描述 给你N(N<=100)个数,请你按照从小到大的顺序输出. 输入 输入数 ...