C#C
C#12mo ago
Core

✅ System.Threading.Channels - Consumer is blocking the main thread where the producer is

Hello,
The producer writes to the channel with WriteAsync(), but the consumer blocks the termination of the producer.
The outcome should be a non-blocking read, so the producer would complete in the meantime.

The following is the consumer:

c#
while (await _reader.WaitToReadAsync())
{ 
    var metadata = await _reader.ReadAsync();
}


c#
public sealed class VisitMetadataChannel
{
    private readonly Channel<VisitMetadata> _channel = Channel.CreateUnbounded<VisitMetadata>(
        new UnboundedChannelOptions
        {
            SingleReader = true, 
            SingleWriter = false, 
            AllowSynchronousContinuations = false
        });
    
    public ChannelWriter<VisitMetadata> Writer => _channel.Writer;
    
    public ChannelReader<VisitMetadata> Reader => _channel.Reader;
}
Was this page helpful?