using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace UnderstandingThreading
{
public class ParallelProcessor2
{
List<CallContext> _contexts;
List<Thread> _workers;
private ManualResetEvent _manualWait = new ManualResetEvent(true);
public ParallelProcessor2()
{
_workers = new List<Thread>();
_contexts = new List<CallContext>();
}
public void AddTask(Action taskCallback, string taskname)
{
lock (this)
{
//handle abort condition
int threadCnt = _contexts.Count;
//_contexts.Add(CallContext.SetData(threadCnt.ToString(), this););
//Create TaskItem
TaskItem tsk = new TaskItem(threadCnt, taskCallback, taskname);
//Spawn new thread
Thread t = new Thread(Consume);
object o = new object();
o = tsk;
if (_workers.Count == 0)
_manualWait.Reset();
_workers.Add(t);
t.Start(o);
}
}
private void Consume(object item)
{
object temp = (object)item;
TaskItem tsk = (TaskItem)temp;
//to do: code to fetch from contexts array by passing identifier name
try
{
tsk.ExecuteTaskItem();
}
catch (Exception ex)
{
//create a method to report exception in call context
//use call context to report exception
}
finally
{
//removing completed threads
}
}
}
public class TaskItem
{
private Action _taskCallback;
private string _taskName;
private int _taskCounter;
public TaskItem(int taskCounter, Action taskCallback, string taskname)
{
_taskCounter = taskCounter;
_taskCallback = taskCallback;
_taskName = taskname;
}
internal void ExecuteTaskItem()
{
_taskCallback();
}
}
}
No comments :
Post a Comment