Click here to Skip to main content
11,925,006 members (54,692 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

A Multi Threaded Linked Task Queue

, 30 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
I needed a multithreaded task queue but with linked tasks, so I created this Multithreaded Task Queue.



The class explained here demonstrates a multi-threaded Linked Task Queue. This class allows you to add multiples tasks to multiple queues. Two queues may run simultaneously in different threads, but each queue will run one task at a time and one after the other.


Once I came across a project where I had to do a lot of processor hungry tasks of copying files, compression and encryption etc. So, I created this easy to use framework to queue tasks and display their status. The demo project here was created in a hurry, and does not demonstrate all the features of the class.



Using the code

To use this code, first of all, add TaskQueue member variables in your form or class, and then in the initialization code, create the queue using the TaskQueueTable class. Like shown below..

protected TaskQueue taskQueue;
public frmQueueDemo()
    taskQueue = TaskQueueTable.Instance.GetTaskQueue("Encryption");
    taskQueue.TaskStarting += 
     new TaskQueue.TaskStartingEeventHandler(taskQueue_TaskStarting);

Then, inherit the Task class to create your classes and override the PerformTask function. Like this..

public class EncriptFileTask : Task
    public EncriptFileTask(string filePath) : 
           base("Encripting file " + filePath)
        this.FilePath = filePath;
    private string filePath;
    public string FilePath {
            return filePath;
            filePath = value;
    protected override void PerformTask(object param)
            FileStream reader = File.OpenRead(FilePath);
            RC4.rc4_key key = new RC4.rc4_key();
                           "Task Queue Demo"), ref key);

            RC4 rc4 = new RC4();
            byte[] read = new byte[8];
            long total = reader.Length;
            long completed = 0;
            while(reader.Position < total && !IsAborting)
                int bRead = reader.Read(read, 0, 8);
                completed += bRead;
                RC4.rc4(ref read, bRead, ref key);
                int percent = (int)((completed / (double)total) * 100);
                //if(percent > 0) OnAddLog("Percent " + percent);
        catch (Exception ex)

Remember to call the base.PerformTask function to be sure to call the proper events when the task finishes. Also remember to use the IsAborting property to allow peaceful cancelation of a task.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
I have been involved in C, C++ development for over a six year now. I have worked on projects involving games, insurance application, stats and reporting applications. Being busy with work I hardly got time to post my own articles but now I am getting more involved. I don't promise much but hopefully you'll see some more interesting articles from me in near future.

You may also be interested in...

Comments and Discussions

QuestionSimple and sensible Pin
Sukar12-Jul-13 9:16
memberSukar12-Jul-13 9:16 
GeneralMake PerformTask abstract Pin
zlezj30-Apr-09 21:59
memberzlezj30-Apr-09 21:59 
GeneralRe: Make PerformTask abstract Pin
Sanjay19822-May-09 22:06
memberSanjay19822-May-09 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 30 Apr 2009
Article Copyright 2009 by Sanjay1982
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid