Code Monkey home page Code Monkey logo

observableconcurrentqueue's Introduction

ObservableConcurrentQueue

ObservableConcurrentQueue


BUILD NuGet Badge

Using System.Collections.Concurrent.ConcurrentQueue with notifications

Get the latest version of source code from Codeplex

Or get it from NUGET:

PM> Install-Package ObservableConcurrentQueue

Documentation

if you are not familiar with ConcurrentQueue, Read more about it on MSDN

Usage

Syntax

Create new instance without thread safe

var observableConcurrentQueue = new ObservableConcurrentQueue();

Create new Thread Safe instance

var observableConcurrentQueue = new ObservableConcurrentQueue(true);

Note about Thread Safety:

According to Mircosoft Documentation All public and protected members of ConcurrentQueue<T> are thread-safe and may be used concurrently from multiple threads. This additional Thread Safe option is just for some customization stuff.

Subscribe the Handler to the event ContentChanged

observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;

Example of handling method:

private static void OnObservableConcurrentQueueContentChanged(
 object sender,
 NotifyConcurrentQueueChangedEventArgs args)
 {
      // Item Added
      if (args.Action == NotifyConcurrentQueueChangedAction.Enqueue)
      {
          Console.WriteLine("New Item added: {0}", args.ChangedItem);
      }
 
      // Item deleted
      if (args.Action == NotifyConcurrentQueueChangedAction.Dequeue)
      {
          Console.WriteLine("New Item deleted: {0}", args.ChangedItem);
      }
 
      // Item peeked
      if (args.Action == NotifyConcurrentQueueChangedAction.Peek)
      {
           Console.WriteLine("Item peeked: {0}", args.ChangedItem);
      }
 
      // Queue Empty
      if (args.Action == NotifyConcurrentQueueChangedAction.Empty)
      {
           Console.WriteLine("Queue is empty");
      }
 } 

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

Event Args

The EventArgs object sent by the event contains 2 properties:

NotifyConcurrentQueueChangedAction Action:

  • Enqueue: If a new item has been enqueued.
  • 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.

Supported Frameworks

.NET Standard

netstandard1.1 netstandard1.2 netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1

.NET Core

netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1

.NET Framework

net40 net45 net451 net452 net46 net461 net462 net47 net471 net472 net48

observableconcurrentqueue's People

Contributors

younescheikh avatar cyounes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.