C#|.NET : Generic Concurrent Queue (1/6)


I think my concurrent process queue could be a good excuse to dive into asynchronous processes, threading, locks, etc. So, I have decided to write about this process queuing mini-system I designed and developed for my Windows Phone app. There are some aspects of this topic which will require multiple posts. So I have also decided to keep it open regarding number of posts to be done to cover the topic. But roughly, I will try to segregate the discussion in following sections:

  • Introduction
  • Design
  • Code
  • Examples
  • Sample Project


Entities (model or viewmodel) require to perform certain long-running tasks in background. Async modifier is one of the easiest ways for achieving this. I wanted to manage my asynchronous request back-log and control it in my app, and without having to bother about threads|locks|pending items etc. I also wanted to have the flexibility to decide, as per scenario, whether method should be executed synchronously or asynchronously (you cannot do this with Async modifier). During the life cycle of the application there could be multiple calls to these methods on a type of entity.
Let’s understand the requirement with a real-time example. In my app Avirall Time Suite, there are about 25 model entities which are to be persisted on local media through JSON serialization. For simplicity, let’s consider one method – Save(). This method is in super class of all model entities and it is meant to serialize the entity on IsolatedStorage. Throughout the life of app, Save is called many times from different places in the app. To free UI thread ASAP, Save() is to be called on background thread. At some places I also need to call Save(), synchronously. For some resource hungry tasks, I pause all the background processes, and then restart them. While exiting the app I need to make sure that all the processes in different background request queues are safely completed or not. Not only for model entities, but I also required process queues for other classes, for example – alerts in the app.
So I designed this process request queue. Let’s call it ConcurrentQueue. Following were the goals for ConcurrentQueue:

  • Process requests in queue
  • Asynchronous (with ability to perform designated processes synchronously)
  • Should be extensible and flexible, so I chose Generics
  • Controllable
    • Auto start
    • Halt
    • Stop
    • Exit
    • Reset
    • Re-prioritize without losing any previous process requests
    • Cancel previous request

To achieve these goals I created a class, which:

  • is an abstract generic type ConcurrentQueue<T> class
  • contains a generic type Queue
  • has an abstract Process method
  • has method to Start, Reset, Restart, Halt, and Exit

Following is the top level view of the queue:


To be continued…[Part 2]

8 thoughts on “C#|.NET : Generic Concurrent Queue (1/6)

  1. Pingback: Generic Concurrent Queue (2/n) | Sharp Statements

  2. Pingback: C#|.NET : Generic Concurrent Queue (3/n) | Sharp Statements

  3. Pingback: C#|.NET : Generic Concurrent Queue (4/n) | Sharp Statements

  4. Pingback: C#|.NET : Generic Concurrent Queue (5/n) | Sharp Statements

  5. Pingback: C#|.NET : Generic Concurrent Queue (6/6) | Sharp Statements

  6. Pingback: XAML|C# Step By Step : UserControl – Wait Spinner | Sharp Statements

  7. Pingback: Does Thread.Sleep affect the ThreadPool? - Popular Windows Phone Questions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s