© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
56 replies
Core

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

Hello,
The producer writes to the channel with
WriteAsync()
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:

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


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;
}
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;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Kafka Flow (Producer Consumer arch.)
C#CC# / help
2y ago
✅ System threading tasks
C#CC# / help
2y ago
✅ Thread and Threading
C#CC# / help
2y ago
❔ Producer Consumer ISourceBlock<T>, periodically flushing consumed data
C#CC# / help
4y ago