Skip to content

Channel Async Streaming #200

Open
@szv

Description

@szv

In the example (shown below), the ChannelClosedException is caught when the channel is closed.
This is not an exception, but the rule.
Instead, the channel reader can also be used in combination with async streaming.

public static Task ReadSomeDataAsync(ChannelReader<SomeData> 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)
{
Console.WriteLine("channel closed");
}
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions