Questions tagged [system.threading.channels]

Provides a set of highly scalable, low/no-allocation synchronization data structures for passing data between producers and consumers asynchronously.


System.Threading.Channels contains the abstractions and base implementation for async friendly queues as a replacement for synchronous thread-safe BlockingCollection and ConcurrentQueue.

It's also a partial replacement for the System.Threading.Tasks.Dataflow async friendly queues (which is implicitly implemented inside BufferBlock and other blocks).

The API Proposal is probably the only official documentation at the moment.

Exploring System.Threading.Channels offers a good overview of channels

Decoupling

Publishers and subscribers can only access a channel through a ChannelWriter and ChannelReader respectively. This decouples publishers, subscribers and the channel implementation itself.

Backpressure

A Bounded Channel allows only a specific number of items. Its behaviour when full depends on the BoundedChannelFullMode option. By default, publishers will have to wait asynchronously if a channel becomes full.

Other values of the BoundedChannelFullMode enum allow different behaviours. For example, DropOldest can be used to create a circular buffer. DropWrite can be used to throttle publishers by rejecting overflowing messages.

65 questions
1
vote
2 answers

How does a channel consumer get everything from multiple channel producers when any producer can close the entire channel at any time?

I'm new to System.Threading.Channels. I have the following consumer code: await foreach (var thing in this.Reader.ReadAllAsync(cancellationToken) .ConfigureAwait(false)) { await this.HandleThingAsync(thing,…
Bob Vesterman
  • 1,127
  • 1
  • 11
  • 31
1
vote
1 answer

ChannelReader.ReadAsync(CancellationToken) and ChannelWriter.WriteAsync(CancellationToken) don't return or throw when token is signaled

EDIT: I'm cleaning up the description because I've since determined this also impacts WriteAsync, not just ReadAsync... If one of these calls is currently blocking - ReadAsync because the channel is empty, or WriteAsync because the channel is full -…
Jeff
  • 495
  • 4
  • 14
1
vote
2 answers

Async method not executing when using System.Threading.Channels

For some reason, it appears code inside of the Consumer nor Producer Tasks is ever executed. Where am I going wrong? using System.Threading.Channels; namespace TEST.CHANNELS { public class Program { public static async Task…
LCaraway
  • 1,257
  • 3
  • 20
  • 48
1
vote
2 answers

How to make count available on .NET Channel

I have an UnboundedChannel single reader\multiple writers and I want to be able to use Count property, but since CanCount property is always false Count is throwing an exception. What should i do to make it work, i can't find any restrictions…
cookieMonster
  • 508
  • 6
  • 15
1
vote
2 answers

Exposing a push via callback message broker as an IAsyncEnumerable

I am working with a third party library which acts as an interface to a pub-sub message broker. The broker is Solace PubSub+. For subscribers, the vendor library employs a "push messages via callback" pattern. I am writing a my own wrapper library…
allmhuran
  • 4,154
  • 1
  • 8
  • 27
1
vote
1 answer

ChannelReader Completion Task is never completed after OperationCanceledException

If I call Stop(), OperationCanceledException is happened and _writer.TryComplete(exp) is true. But _reader.Completion Task is still not completed. Is it desired behavior of Channels? If yes can someone tell me how to stop a Channel without waiting…
d.lavysh
  • 1,404
  • 14
  • 23
1
vote
1 answer

When using Channels with SignalR server-to-client streaming, is the server-side Complete guaranteed to be delivered to the client?

I'm doing SignalR server-to-client streaming using System.Threading.Channel with a .NET client. The usage is fairly basic, similar to what is described in the introductory docs. The hub code is similar to this: public ChannelReader
koopaking3
  • 3,375
  • 2
  • 25
  • 36
1
vote
1 answer

Why my workers work distribution count does not total the number of produced items in this System.Threading.Channel sample?

Following this post, I have been playing with System.Threading.Channel to get confident enough and use it in my production code, replacing the Threads/Monitor.Pulse/Wait based approach I am currently using (described in the referred post). Basically…
Veverke
  • 9,208
  • 4
  • 51
  • 95
1
vote
1 answer

System.Threading.Channels ReadAsync() method is blocking execution

Overview I am attempting to write an IAsyncEnumerable wrapper around an IObserver interface. At first I used a BufferBlock as the backing data store, but I found out through performance testing and research that it is actually a pretty slow…
1
vote
1 answer

What might ChannelReader.ReadAny implementation look like?

BlockingCollection has the handy static TakeFromAny method allowing you to consume multiple collections "I want the next item from any of these collections". ChannelReader doesn't have an equivalent so if you did want to consume multiple…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
1
vote
3 answers

how can i implement a concurrent execution queue class?

I need to have a class that will execute actions in thread pool, but these actions should be queued. For example: method 1 method 2 method 3 When someone called method 1 from his thread he can also call method 2 or method 3 and they all 3 methods…
1
vote
0 answers

How can i incorporate event handlers in my streaming app with signalr?

i know basics of c# and asp.net core, however i need some help with event handlers. Im trying to build an asp.net core app which streams tweets to the front-end. Im using tweetinvi library for getting tweets and signalr to do the streaming. I tested…
Avalon
  • 201
  • 1
  • 2
  • 4
1
vote
1 answer

How to scale out a SignalR ChannelReader?

I'm using SignalR 1.0.4 and have a hub that returns a ChannelReader created from an observable by an extension. A typescript client (also 1.0.4) is forced to connect using websockets only, and streams data from this channel fine. Now I'm testing…
Mark
  • 1,059
  • 13
  • 25
0
votes
0 answers

How to notify the FileSystemWatcher that file copy either from same machine or another machine completes and start processing

I have below program where StartFileWatcher looking into a folder (C:\TEMP) (including subdirectories) and copying all the files info to the C# channel based of file LastWriteTimeUtc and having some logic to compare. This works most of the case (all…
user584018
  • 10,186
  • 15
  • 74
  • 160
0
votes
1 answer

When should System.Threading.Channels be preferred to ConcurrentQueue?

I recently built a consumer/producer system using ConcurrentQueue and SemaphoreSlim. Then made another alternative system utilizing the new System.Threading.Channel class. After benchmarking both systems using BenchmarkDotNet by writing 1000…