Observable Concurrent Queue
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
- Source: Github Ripository
- Binary: Nuget :
PM> Install-Package ObservableConcurrentQueue
Originally published at cyounes.com on January 2, 2015.