Observable Concurrent Queue

Younes Cheikh
2 min readJan 2, 2015

--

I was working with the classic ConcurrentQueue in .Net Framework, It’s a good practice to schedule tasks execution asynchronously, however, getting notified when my ConcurrentQueue content is changed was the problem! How can i know if an element has been enqueued, peeked or dequeued? Also how can i be notified when the queue is empty after last element has been dequeued?

To solve the problem of notification, I've developed my custom ConcurrentQueue named ObservableConcurrentQueue inherits from System.Collections.Concurrent.ConcurrentQueue to raise events once the content is changed.

If you are not familiar with the ConcurrentQueue, Please read more about it on MSDN

Syntax & Example:

The handle-Method ContentChanged event looks as the following:

Then, Once the handler is defined, we can start adding, deleting or getting elements from the ConcurrentQueue, so after each operation an event will be raised and handled by the handler above.

Event Args:

The EventArgs object sent by the event contains 2 properties:

NotifyConcurrentQueueChangedAction

  • Enqueue: If a new item has been en-queued.
  • Dequeue: an item has been dequeued.
  • Peek: an item has been peeked.
  • Empty: The last element in the queue has been dequeued and the queue is empty.

T ChangedItem

The item which the changes applied on. can be null if the notification action is NotifyConcurrentQueueChangedAction.Empty.

Download

PM> Install-Package ObservableConcurrentQueue

Originally published at cyounes.com on January 2, 2015.

--

--

Younes Cheikh
Younes Cheikh

Written by Younes Cheikh

Software Engineer, specialized in Object-Oriented Design, Algorithms and Analysis. I write from time to time :)

No responses yet