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
0
votes
2 answers

Fast producer, slow consumer, bounded channel, reduce the frequency of awakening the producer

I have a producer-consumer scenario¹ based on a bounded Channel. Channel channel = Channel.CreateBounded(10); The items are coming from an RDBMS, to which the producer connects and fetches them one by one. The peculiarity is that I…
0
votes
1 answer

How to pass arguments into a Channel's queue writer?

I have a background task to be completed; however, I can't write arguments to the Channel. Reason being that the writer only takes in 1 argument. How do queue a function up to be completed with arguments. private readonly Channel
CorrieJanse
  • 2,374
  • 1
  • 6
  • 23
0
votes
4 answers

Producer consumer using a bounded Channel, with strict memory allocation requirements

Here is my scenario¹. I have a producer consumer system that is composed by two producers, one consumer, and a bounded Channel configured with capacity 2. The T is byte[]. Channel channel = Channel.CreateBounded(2); The byte[]s…
0
votes
2 answers

Using Task.WhenAny to await capacity on a SemaphoreSlim

I have an Async processing pipeline. I'm implementing a constraint such that I need to limit the number of submissions to the next stage. For my component, I have: a single input source (items are tagged with a source id) a single destination that…
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
0
votes
1 answer

How to batch a ChannelReader, enforcing a maximum interval policy between consuming and processing any individual item?

I am using a Channel in a producer-consumer scenario, and I have the requirement to consume the channel in batches of 10 items each, and without letting any consumed item to stay idle in a buffer for more than 5 seconds. This duration is the…
0
votes
1 answer

How to not propagate Activity in Task.Factory.StartNew

I have the following code: Task task = null; var channel = System.Threading.Channels.Channel.CreateUnbounded(); using (var activity = MyActivitySource.StartActivity("Parent")) { task = Task.Factory.StartNew(async () => { …
0
votes
1 answer

Channel Writer requiring Task.Delay and not processing all messages

I am adding messages to a threading channel and reading from this channel constantly. I noticed that if I do not add a Task.Delay then all of the messages are not processed. The program will exit with maybe 10 messages processed when it should be…
0
votes
2 answers

Given an external producer API that can be stopped and started, efficiently stop the producer when local buffer is full

Suppose I am provided with an event producer API consisting of Start(), Pause(), and Resume() methods, and an ItemAvailable event. The producer itself is external code, and I have no control over its threading. A few items may still come through…
allmhuran
  • 4,154
  • 1
  • 8
  • 27
0
votes
0 answers

Using multiple channels, what am I doing wrong?

I want to create an array of Tasks, called listTask, with each element of listTask is a Task of type A, Task of type A is created by the function Task.WhenAll. then I do await Task.WhenAll(listTask) But the program does not perform the work in the…
0
votes
2 answers

How to complete a Channel right way? How to use multiple channels?

I have a list of data and want to create the number of Tasks corresponding to the number of elements in the list. But I don't know how to Complete a Channel properly. My code, but the Channel doesn't close as I expect. using System; using…
0
votes
1 answer

Using VS 2019 Worker Template, a Background service is quitting

Using .Net core 3.1 and the Visual Studio 2019 Worker Template. I followed example code found online. The Background task MessageReadingService reads a message from Amazon SQS. The message is written to a ChannelReader. The Background task…
user471230
  • 81
  • 7
0
votes
0 answers

if database call fails, can we move the item back to Channel

I am using Channel from system.threading.channels in asp.net core 3.1 application where I am continuously writing into a channel and reading from it. When I am reading a item from channel, I am making a database call to save the item. Questions is,…
user584018
  • 10,186
  • 15
  • 74
  • 160
0
votes
0 answers

State management with Channels

I am currently working with a Channels construct (like go routine) to manage multi-threaded state synchronization. I'm curious as to the best practice on how to handle different operations for state management using that channel. One operation could…
Dane Balia
  • 5,271
  • 5
  • 32
  • 57
0
votes
2 answers

Allocation-free timeout on Task

I'd like to add a timeout to ChannelReader.ReadyAsync. Here are two solutions I've found: var cts = new CancellationTokenSource(); cts.CancelAfter(2000); try { var data = chan.ReadAsync(cts.Token); } catch (OperationCanceledException) { //…
0
votes
3 answers

Retain variable values while running 2 or more tasks in parallel

I am working with timers and have implemented threading as first step in solving my problem. I have to send some reference every 30 seconds and this reference is created everytime a button is clicked. So I have the following code: public MyReference…