diff --git a/2_Libs/Parallel/ParallelSamples/ChannelSample/ChannelSample.cs b/2_Libs/Parallel/ParallelSamples/ChannelSample/ChannelSample.cs index 7abd269a..725fcfc5 100644 --- a/2_Libs/Parallel/ParallelSamples/ChannelSample/ChannelSample.cs +++ b/2_Libs/Parallel/ParallelSamples/ChannelSample/ChannelSample.cs @@ -26,23 +26,18 @@ public static Task WriteSomeDataWithTryWriteAsync(ChannelWriter writer public static Task ReadSomeDataAsync(ChannelReader reader) => Task.Run(async () => { - try - { - Console.WriteLine("Start reading..."); - Random r = new(); - while (true) - { - await Task.Delay(r.Next(80)); - var data = await reader.ReadAsync(); - Console.WriteLine($"read: {data.Text}, available items: {reader.Count}"); - } - } - catch (ChannelClosedException) + Random r = new(); + Console.WriteLine("Start reading..."); + + await foreach (var data in reader.ReadAllAsync()) { - Console.WriteLine("channel closed"); + await Task.Delay(r.Next(80)); + Console.WriteLine($"read: {data.Text}, available items: {reader.Count}"); } + + Console.WriteLine("channel closed"); }); - + public static Task WriteSomeDataAsync(ChannelWriter writer) => Task.Run(async () => @@ -57,25 +52,20 @@ public static Task WriteSomeDataAsync(ChannelWriter writer) => } writer.Complete(); Console.WriteLine("Writing completed"); - }); + }); - public static Task ReadSomeDataUsingAsyncStreams(ChannelReader reader) => + public static Task ReadSomeDataUsingAsyncStreams(ChannelReader reader) => Task.Run(async () => { - try - { - Console.WriteLine("Start reading..."); - Random r = new(); - await foreach (var data in reader.ReadAllAsync()) - { - await Task.Delay(r.Next(80)); - Console.WriteLine($"read: {data.Text} available items: {reader.Count}"); - } + Random r = new(); + Console.WriteLine("Start reading..."); - } - catch (ChannelClosedException) + await foreach (var data in reader.ReadAllAsync()) { - Console.WriteLine("channel closed"); + await Task.Delay(r.Next(80)); + Console.WriteLine($"read: {data.Text} available items: {reader.Count}"); } - }); + + Console.WriteLine("channel closed"); + }); }