Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel Async Streaming #200

Open
szv opened this issue Jan 2, 2024 · 0 comments · May be fixed by #201
Open

Channel Async Streaming #200

szv opened this issue Jan 2, 2024 · 0 comments · May be fixed by #201

Comments

@szv
Copy link

szv commented Jan 2, 2024

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");
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant