Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. private async Task WriteItemsAsync(
  2. ChannelWriter<int> writer,
  3. int count,
  4. int delay,
  5. CancellationToken cancellationToken)
  6. {
  7. try
  8. {
  9. for (var i = 0; i < count; i++)
  10. {
  11. // Check the cancellation token regularly so that the server will stop
  12. // producing items if the client disconnects.
  13. cancellationToken.ThrowIfCancellationRequested();
  14. await writer.WriteAsync(i);
  15.  
  16. // Use the cancellationToken in other APIs that accept cancellation
  17. // tokens so the cancellation can flow down to them.
  18. await Task.Delay(delay, cancellationToken);
  19. }
  20. }
  21. catch (Exception ex)
  22. {
  23. writer.TryComplete(ex);
  24. }
  25.  
  26. writer.TryComplete();
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement