Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

A Multi Threaded Linked Task Queue

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
30 Apr 2009CPOL 37.5K   866   36   3
I needed a multithreaded task queue but with linked tasks, so I created this Multithreaded Task Queue.

Task_Queue_Demo.jpg

Introduction

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.

Background

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.

Classes

ClassDiagram1.jpg

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..

C#
protected TaskQueue taskQueue;
public frmQueueDemo()
{
    InitializeComponent();
    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..

C#
public class EncriptFileTask : Task
{
    public EncriptFileTask(string filePath) : 
           base("Encripting file " + filePath)
    {
        this.FilePath = filePath;
    }
    private string filePath;
    public string FilePath {
        get
        {
            return filePath;
        }
        set
        {
            filePath = value;
        }
    }
    protected override void PerformTask(object param)
    {
        try
        {
            FileStream reader = File.OpenRead(FilePath);
            RC4.rc4_key key = new RC4.rc4_key();
            RC4.PrepareKey(System.Text.UTF8Encoding.ASCII.GetBytes(
                           "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);
                OnProgress(percent);
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            OnAddLog(ex.Message);
        }
        base.PerformTask(param);
    }
}

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.

License

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


Written By
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.

Comments and Discussions

 
QuestionSimple and sensible Pin
Sukar12-Jul-13 8:16
Sukar12-Jul-13 8:16 
GeneralMake PerformTask abstract Pin
zlezj30-Apr-09 20:59
zlezj30-Apr-09 20:59 
GeneralRe: Make PerformTask abstract Pin
Sanjay19822-May-09 21:06
Sanjay19822-May-09 21: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.