// // System.Threading.Channels.dll // System.Threading.Tasks.Extensions.dll // System // System.Threading // System.Threading.Channels // System.Threading.Tasks // using System; using System.Threading; using System.Threading.Tasks; using System.Threading.Channels; class Program { Func tid = () => Thread.CurrentThread.ManagedThreadId; static BoundedChannelOptions one2one = new BoundedChannelOptions (1) {SingleWriter=true, SingleReader=true,}; Channel achan = Channel.CreateBounded (one2one); async Task agent () { var count = 0; for (;;) { var msg = await achan.Reader.ReadAsync (); count += 1; Console.WriteLine ("[{0}] agent received {1}, total={2} messages", tid(), msg, count); await Task.Delay (100); Console.WriteLine ("[{0}] agent ...", tid()); } } async Task Main2() { Console.WriteLine ("[{0}] main", tid()); var a = agent (); foreach (var m in new[] {"the", "quick", "brown", "fox",}) { await achan.Writer.WriteAsync (m); } foreach (var m in new[] {"jumps", "over", "the", "lazy", "dog",}) { await achan.Writer.WriteAsync (m); } Console.WriteLine (" to exit"); Console.ReadLine (); } static void Main() { new Program().Main2().Wait(); } }