WorkerThread包装类

WorkerThread类是围绕基本.NET线程类的一个高层包装类。WorkerThread类定义为:

public class WorkerThread : IDisposable {

       public WorkerThread();

       public WorkerThread(bool autoStart);

       public int ManagedThreadId {get;}

       public Thread Thread {get;}

       public WaritHandle Handle {get;}

       public void Start();

       public void Dispose();

       public void Kill();

       public void Join();

       public bool Join(int millisecondsTimeout);

       public bool Join(TimeSpan timeout);

       public string Name {get;set;}

 

public bool IsAlive {get;}

public void Dispose();

}

WorkerThread提供了简单的线程创建和其他特性,包括终止线程的Kill()方法(而不是使用Abort())Thread类中的一些有潜在危险的方法没包含在WorkerThread的接口中,但是好的方法仍然保留了下来。WorkerThread还强调我们已经讨论过的关于使用.NET线程的最佳实践。下面的示例说明了WorkerThread的执行。由于Thread类是sealed的,在定义WorkerThread时,必须使用包含(containment)而不是派生。WorkerThread拥有Thread类型的m_ThreadObj成员变量,代表了低层的被包装的线程。如果想要进行直接线程操作,可以通过WorkerThreadThread属性访问底层线程。

WorkerThread包装类

public class WorkerThread : IDisposable {

       ManualResetEvent m_ThreadHandle;

       Thread m_ThreadObj;

       bool m_EndLoop;

       Mutext m_EndLoopMutex;

 

       public override int GetHashCode() {

              return m_ThreadObj.GetHashCode();

}

public override bool Equals(object obj) {

       return m_ThreadObj.Equals(obj);

}

public int ManagedThreadId {

       get {

              return m_ThreadObj.ManagedThreadId;

}

}

public Thread Thread {

       get {

              return m_ThreadObj;

}

}

protected bool EndLoop {

       set {

              m_EndLoopMutex.WaitOne();

              m_EndLoop = value;

              m_EndLoopMutex.ReleaseMutex();

}

get {

       bool result = false;

       m_EndLoopMutex.WaitOne();

       result = m_EndLoop;

       m_EndLoopMutex.ReleaseMutex();

       return result;

}

}

public WokerThread() {

       m_EndLoop = false;

       m_ThreadObj = null;

       m_EndLoopMutex = new Mutex();

       m_ThreadHandle = new ManualResetEvent(false);

       m_ThreadObj = new Thread(Run);

       Name = "Worker  Thread";

}

public WorkerThread(bool autoStart) : this() {

       if(autoStart) {

              Start();

}

}

public WaitHandle Handle {

       get {

              return m_ThreadHandle;

}

}

public void Start() {

       Debug.Assert(m_ThreadObj != null);

       Debug.Assert(m_ThreadObj.IsAlive == false);

       m_ThreadObj.Start();

}

public void Dispose() {

       Kill();

}

void Run() {

       try {

              int i = 0;

              while(EndLoop == false) {

                     Trace.WriteLine("Thread is alive, Counter is "+i);

       i++;

}

}

finally {

       m_ThreadHandle.Set();

}

}

public void Kill() {

       //Kill()在客户线程那里被调用,必须使用缓存的Thread对象

       Debug.Assert(m_ThreadObj != null);

       if(IsAlive == false) {

              return;

}

EndLoop = true;

//等待线程死亡

Join();

m_EndLoopMutex.Close();

m_ThreadHandle.Close();

}

public void Join() {

       Join(Timeout.Infinite);

}

public bool Join(int millisecondsTimeout) {

       TimeSpan timeout =

TimeSpan.FromMilliseconds(millisecondsTimeout);

       return Join(timeout);

}

public bool Join(TimeSpan timeout) {

       //Join()在客户线程被调用,必须使用缓存的Thread对象

       Debug.Assert(m_ThreadObj != null);

       if(IsAlive == false) {

              return true;

}

Debug.Assert(Thread.CurrentThread.ManagedThreadIsd !=

m_ThreadObj.ManagedThreadId);

              return m_ThreadObj.Join(timeout);

}

public string Name {

       get {

              return m_ThreadObj.Name;

}

set {

       m_ThreadObj.Name = value;

}

}

public bool IsAlive {

       get {

              Debug.Assert(m_ThreadObj != null);

              bool handleSignaled = m_ThreadHandle.WaitOne(0,true);

              while(handleSignaled == m_ThreadObj.IsAlive) {

                     Thread.Sleep(0);

}

return m_ThreadObj.IsAlive;

}

}

}

 
 
posted @ 2012-12-13 15:47  星火卓越  阅读(265)  评论(0编辑  收藏  举报