Click here to Skip to main content
Click here to Skip to main content

A Multi Threaded Linked Task Queue

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

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)

Share

About the Author

Sanjay1982
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 PinmemberSukar12-Jul-13 8:16 
GeneralMake PerformTask abstract Pinmemberzlezj30-Apr-09 20:59 
GeneralRe: Make PerformTask abstract PinmemberSanjay19822-May-09 21:06 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140826.1 | Last Updated 30 Apr 2009
Article Copyright 2009 by Sanjay1982
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid