Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 877 Bytes

08-channels.md

File metadata and controls

44 lines (31 loc) · 877 Bytes

08 - Channels

Goal

Use Channel<T> for modern asynchronous producer/consumer workflows.

Task

Improve the background queue using:

System.Threading.Channels

Step-by-step

  1. Create a bounded channel.
  2. Configure BoundedChannelOptions.
  3. Use WriteAsync for producers.
  4. Use ReadAsync for consumers.
  5. Add graceful completion.
  6. Add tests for cancellation and backpressure.

Important options

new BoundedChannelOptions(capacity)
{
    FullMode = BoundedChannelFullMode.Wait,
    SingleReader = false,
    SingleWriter = false
};

Questions to answer

  • What is backpressure?
  • What happens when the channel is full?
  • What is the difference between TryWrite and WriteAsync?
  • When should the writer be completed?

Key takeaway

Channel<T> is a strong default for async producer/consumer code in modern .NET.