Click here to Skip to main content
Licence CPOL
First Posted 30 Apr 2009
Views 12,765
Downloads 228
Bookmarked 31 times

A Multi Threaded Linked Task Queue

By | 30 Apr 2009 | Article
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..

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

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)

About the Author

Sanjay1982

Software Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMake PerformTask abstract Pinmemberzlezj20:59 30 Apr '09  
GeneralRe: Make PerformTask abstract PinmemberSanjay198221:06 2 May '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 30 Apr 2009
Article Copyright 2009 by Sanjay1982
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid