using System; using System.Linq; using System.Collections.Generic; using System.Collections.Concurrent; using System.Reflection; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; var channel = System.Threading.Channels.Channel.CreateBounded(2); if (!channel.Writer.TryWrite(2) || !channel.Writer.TryWrite(4)) throw new InvalidOperationException(); channel.Writer.TryComplete(new InvalidOperationException("controlled")); var items = new List(); while (channel.Reader.TryRead(out int item)) items.Add(item); Console.WriteLine("items:" + string.Join(",", items)); try { await channel.Reader.Completion; } catch (InvalidOperationException error) { Console.WriteLine($"failure:{error.GetType().Name}"); } Console.WriteLine($"write-after:{channel.Writer.TryWrite(6)}");