Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Parallel Tasks in .NET 3.0

Rate me:
Please Sign up or sign in to vote.
4.89/5 (9 votes)
6 May 2010CPOL4 min read 33.8K   468   43  
Provide a mechanism to execute a list of tasks in parallel on multiple threads and communicate back to the calling thread useful states such as exceptions, timeouts, and successful task completion.
using System;
using System.Threading;

namespace ParallelTaskRunner
{
    public class ThreadTaskInfo<T>
    {
        private readonly object _synchLock = new object();

        /// <summary>
        /// Initializes a new instance of the <see cref="ThreadTaskInfo&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="task">The task.</param>
        public ThreadTaskInfo(T task)
        {
            Task = task;

            // Task is created, but has not started running yet
            State = ThreadTaskStateType.Created;

            // Task is not set to completed
            TaskComplete = new ManualResetEvent(false);
        }

        /// <summary>
        /// Gets or sets the task that is running on the thread
        /// </summary>
        /// <value>The task.</value>
        public T Task { get; private set; }

        /// <summary>
        /// Gets or sets the task complete signal (Wait Handle).
        /// </summary>
        /// <value>The task complete.</value>
        public ManualResetEvent TaskComplete { get; private set; }

        /// <summary>
        /// Gets or sets an exception if it is thrown by the task.
        /// </summary>
        /// <value>The exception.</value>
        public Exception Exception { get; set; }

        /// <summary>
        /// Gets or sets the state of currently running threaded task.
        /// </summary>
        /// <value>The state.</value>
        public ThreadTaskStateType State { get; private set; }

        /// <summary>
        /// Set the state of the task.
        /// </summary>
        /// <param name="state">The state.</param>
        public void SetState(ThreadTaskStateType state)
        {
            // Lock the writing of the State property
            lock (_synchLock)
            {
                switch (state)
                {
                    case ThreadTaskStateType.Created:
                        throw new Exception("State cannot be set to created");

                    case ThreadTaskStateType.Started:
                        if (State != ThreadTaskStateType.Created)
                        {
                            return;
                        }
                        State = state;
                        return;

                    case ThreadTaskStateType.Completed:
                        if (State != ThreadTaskStateType.Started)
                        {
                            return;
                        }
                        State = state;
                        return;

                    case ThreadTaskStateType.Failed:
                        if (State == ThreadTaskStateType.Started || State == ThreadTaskStateType.Completed)
                        {
                            State = state;
                        }
                        return;

                    case ThreadTaskStateType.FailedTimeout:
                        if (State == ThreadTaskStateType.Started || State == ThreadTaskStateType.Completed)
                        {
                            State = state;
                        }
                        return;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Australia Australia
I have been programming commercially since 1990, my last two major roles have been Architect/Lead Developer for an online bank and Australia's largest consumer finance comparison portal.

On the side I am a Forex Currency Trader and actively develop tools and applications for Currency Traders.

I have just launched a personal blog at www.davidcruwys.com and a website targeting Foreign Exchange traders at www.my-trading-journal.com

Comments and Discussions