How to dequeue when new item in queue
.net, c#, queue
Solution
Yes. Take a look at TPL Dataflow, in particular, the `BufferBlock<T>`, which does more or less the same as `BlockingCollection` without the nasty side-effect of jamming up your threads by leveraging `async/await`.
So you can:
void Main()
{
var b = new BufferBlock<string>();
AddToBlockAsync(b);
ReadFromBlockAsync(b);
}
public async Task AddToBlockAsync(BufferBlock<string> b)
{
while (true)
{
b.Post("hello");
await Task.Delay(1000);
}
}
public async Task ReadFromBlockAsync(BufferBlock<string> b)
{
await Task.Delay(10000); //let some messages buffer up...
while(true)
{
var msg = await b.ReceiveAsync();
Console.WriteLine(msg);
}
}
Problem
I've an application that works with a queue with strings (which corresponds to different tasks that application needs to perform). At random moments the queue can be filled with strings (like several times a minute sometimes but it also can take a few hours. Till now I always had a timer that checked every few seconds the queue whether there were items in the queue and removed them. I think there must be a nicer solution than this way. Is there any way to get an event or so when an item is added to the queue?