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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With