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:
- 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
- Auto start
- 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]