Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async Task WriteItemsAsync(
- ChannelWriter<int> writer,
- int count,
- int delay,
- CancellationToken cancellationToken)
- {
- try
- {
- for (var i = 0; i < count; i++)
- {
- // Check the cancellation token regularly so that the server will stop
- // producing items if the client disconnects.
- cancellationToken.ThrowIfCancellationRequested();
- await writer.WriteAsync(i);
- // Use the cancellationToken in other APIs that accept cancellation
- // tokens so the cancellation can flow down to them.
- await Task.Delay(delay, cancellationToken);
- }
- }
- catch (Exception ex)
- {
- writer.TryComplete(ex);
- }
- writer.TryComplete();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement