|
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<T>"/> 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.
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