关于CSDN上下载的ThreadManager,请高手帮忙解析一下!

iloverenne 2009-04-25 04:51:06
在论坛上下载了一个c#的ThreadManager,但由于水平有限,加之没有注释,所以想请高手对这些代码加上注释解析一下
不胜感谢!代码如下:
public delegate void ThreadActivationCallback();
public delegate void JobFinishCallback(object state);
public delegate bool OnErrorCallback(Exception exc);

public class ThreadManager
{

public event ThreadStart ThreadStarted;
public event ThreadStart ThreadFinished;
public event WaitCallback JobStarted;
public event WaitCallback JobFinished;
public event OnErrorCallback OnError;
public event ThreadStart Idle;
public uint MaxThreads;
internal int activeThreads = 0;
internal Stack workItems;
internal int threadTimeout = 5000; // 5 seconds
private Timer timer;
public string Name;

public void ClalThreadPool(string name, uint maxThreads)
{
Name = name;
MaxThreads = maxThreads;
workItems = Stack.Synchronized(new Stack((int)(10 * maxThreads)));
timer = new Timer(new TimerCallback(OnTimer), null, 0, 2000);
}

public bool QueueUserWorkItem(WaitCallback callback, object state)
{
if (callback == null)
return false;
lock (this)
{
workItems.Push(new WorkItem(callback, state));
if (activeThreads == 0)
WorkThread.AddWorker(this);
return true;
}

}

public void OnTimer(object state)
{
lock (this)
{
if (workItems.Count == 0)
return;

if (activeThreads < MaxThreads)
WorkThread.AddWorker(this);
}

}

internal void ThreadStartedEvent()
{
if (ThreadStarted != null)
ThreadStarted();
}

internal void ThreadFinishedEvent()
{
if (ThreadFinished != null)
ThreadFinished();
}

internal void JobStartedEvent(object parameter)
{
if (JobStarted != null)
JobStarted(parameter);
}

internal void JobFinishedEvent(object parameter)
{
if (JobFinished != null)
JobFinished(parameter);
}

internal void IdleEvent()
{
if (Idle != null)
Idle();
}

internal bool OnErrorEvent(Exception exc)
{
if (OnError != null)
return OnError(exc);
return false;

}

public void Stop()
{
lock (this)
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
workItems.Clear();
MaxThreads = 0;
}

}

}

internal class WorkItem
{
public object parameter;
public WaitCallback callback;
public WorkItem(WaitCallback callback, object state)
{
this.parameter = state;
this.callback = callback;
}

}

internal class WorkThread
{
ThreadManager pool;
internal WorkThread(ThreadManager pool)
{
this.pool = pool;
}

static public void AddWorker(ThreadManager pool)
{
Interlocked.Increment(ref pool.activeThreads);
WorkThread worker = new WorkThread(pool);
Thread thread = new Thread(new ThreadStart(worker.Loop));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

private void Loop()
{
try
{
pool.ThreadStartedEvent();

while (true)
{
WorkItem item = null;
try
{
item = (WorkItem)pool.workItems.Pop();
}

catch
{
Thread.Sleep(pool.threadTimeout);
item = (WorkItem)pool.workItems.Pop();
if (item == null)
break;
}
try
{
pool.JobStartedEvent(item.parameter);
/*
Write your functions you want to process
* Here we cast the item.parameter into the
* ProcessingSteps and get the type to run
* According to the type related functions
* will be called.
* E.g. if it is CreateProcess then
* CreatePrcess of ProcessEngine will be called.
* Same way functions called for each parameter.
*
*/
//item.callback(item.parameter);
// Following is my function and it is called by passing parameter.
//ProcessingManager.Process((Processingstep)item.parameter);
pool.JobFinishedEvent(item.parameter);
}

catch (Exception exc)
{
try
{
EngineLog.UpdateLogLine(exc.ToString());
pool.OnErrorEvent(exc);
}

catch (Exception critical)
{
// Check for critical exception -- pseudo code
//if (critical is CRITICAL)
throw critical;
}

}

if (pool.activeThreads > pool.MaxThreads)
return;
}

}

catch { }
finally
{
Interlocked.Decrement(ref pool.activeThreads);
}
pool.ThreadFinishedEvent();
if (pool.activeThreads == 0)
pool.IdleEvent();

}
}
...全文
83 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
iloverenne 2009-04-26
  • 打赏
  • 举报
回复
没人帮忙下么。。。。
iloverenne 2009-04-25
  • 打赏
  • 举报
回复
想知道下大概的思想,主要部分的意思。
哈哈潜伏哥 2009-04-25
  • 打赏
  • 举报
回复
你这个太多了,不好搞啊,不如你直接问哪句什么意思算了
陌上花花 2009-04-25
  • 打赏
  • 举报
回复
帮顶了。
关注中....

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧