Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dequeue when new item in queue

Tags:

c#

.net

queue

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?

like image 664
Stefan1991 Avatar asked Feb 13 '23 19:02

Stefan1991


2 Answers

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);
    }
}
like image 153
spender Avatar answered Feb 15 '23 10:02

spender


I'd take a look at BlockingCollection.GetConsumingEnumerable. The collection will be backed with a queue by default, and it is a nice way to automatically take values from the queue as they are added using a simple foreach loop.

There is also an overload that allows you to supply a CancellationToken meaning you can cleanly break out.

like image 25
Daniel Kelley Avatar answered Feb 15 '23 10:02

Daniel Kelley