Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try Dequeue in ConcurrentQueue

The TryDequeue in ConcurrentQueue will return false if no items in Queue.

If the Queue is empty I need that my queue will wait until new item to be added in queue and it dequeue that new one, and the process will continues like that.

Should i use monitor.enter,wait,pulse or any better options in C# 4.0

like image 570
C-va Avatar asked Feb 16 '11 08:02

C-va


People also ask

What is TryDequeue C#?

TryDequeue tries to remove an element from the queue. If the method is successful, the item is removed and the method returns true ; otherwise, it returns false . That happens atomically with respect to other operations on the queue.

When to use ConcurrentQueue?

Use ConcurrentQueue or ConcurrentStack if you need to access the collection from multiple threads concurrently. If you use a Queue ( i.e., not the ConcurrentQueue ) with more than one thread updating the object instance, you could encounter run time exceptions, such as: ArgumentOutOfRangeException.

What is ConcurrentQueue in c#?

C# ConcurrentQueue is a thread-safe collection class. It is introduced in .NET 4.0 with other concurrent collection classes. It provides a thread-safe First-In-First-Out (FIFO) data structure. You can read more about Queue here. ConcurrentQueue exists in System.Collections.Concurrent namespace.


1 Answers

Isn't this what BlockingCollection is designed for?

As I understand it, you can wrap your ConcurrentQueue with one of these, and then call Take.

like image 100
Damien_The_Unbeliever Avatar answered Oct 05 '22 23:10

Damien_The_Unbeliever