Exemplo n.º 1
0
        public void UC02_TenAsyncConsumers()
        {
            // Create a BufferBlock<byte[]> object. This object serves as the
            // target block for the producer and the source block for the consumer.
            var buffer = new BufferBlock <byte[]>();
            var Dpc    = new DataflowProducerConsumer();

            // Start the consumer. The Consume method runs asynchronously.
            List <Task> consumers = new List <Task>();

            for (int j = 1; j <= 10; j++)
            {
                consumers.Add(Dpc.ConsumeAsyncAndTryReceive(buffer));
            }


            // Post source data to [the BufferBlock].
            Dpc.Produce(buffer);

            // Wait for the consumer to process all data.
            foreach (Task consumer in consumers)
            {
                consumer.Wait();
            }

            // Print the count of bytes processed to the console.
            int i = 1;

            foreach (Task <int> consumer in consumers)
            {
                Console.WriteLine("Consumer " + i + " Processed {0} bytes.", consumer.Result);
                i++;
            }
        }
Exemplo n.º 2
0
        public void UC01_OneAsyncConsumers()
        {
            // Create a BufferBlock<byte[]> object. This object serves as the
            // target block for the producer and the source block for the consumer.
            var buffer = new System.Threading.Tasks.Dataflow.BufferBlock <byte[]>();
            var Dpc    = new DataflowProducerConsumer();

            // Start the consumer. The Consume method runs asynchronously.
            var consumer = Dpc.ConsumeAsync(buffer);

            // Post source data to the dataflow block.
            Dpc.Produce(buffer);

            // Wait for the consumer to process all data.
            consumer.Wait();

            // Print the count of bytes processed to the console.
            Console.WriteLine("Processed {0} bytes.", consumer.Result);
        }