Skip to content

Instantly share code, notes, and snippets.

@fgasperij
Last active February 24, 2017 20:31
Show Gist options
  • Save fgasperij/67e64c66ad0245df7005620ae3d85858 to your computer and use it in GitHub Desktop.
Save fgasperij/67e64c66ad0245df7005620ae3d85858 to your computer and use it in GitHub Desktop.
Dataflow PropagateCompletion test
using System;
using System.Linq;
using System.Threading.Tasks.Dataflow;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BufferBlock<int> A = new BufferBlock<int>(new DataflowBlockOptions() { BoundedCapacity = 10 });
TransformBlock<int, int> B = new TransformBlock<int, int>(
(n) =>
{
Thread.Sleep(TimeSpan.FromMilliseconds(1000));
Console.WriteLine($"B: Event {n} is going through.");
return n;
},
new ExecutionDataflowBlockOptions() { BoundedCapacity = 2 });
ActionBlock<int> C = new ActionBlock<int>(
(n) =>
{
Thread.Sleep(TimeSpan.FromMilliseconds(1000));
Console.WriteLine($"C: Event {n} is going through.");
},
new ExecutionDataflowBlockOptions() { BoundedCapacity = 2 });
var propagateCompletion = new DataflowLinkOptions() { PropagateCompletion = true };
A.LinkTo(B, propagateCompletion);
B.LinkTo(C, propagateCompletion);
foreach (int i in Enumerable.Range(0, 10))
{
A.Post(i);
}
A.Complete();
C.Completion.Wait();
Console.WriteLine("C finished.");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment