当前位置:编程学习 > C#/ASP.NET >>

线程状态及其转换

线程对象使用ThreadState.属性指示线程状态。

ThreadState是带flag特性的枚举类型对象,因此判断线程当前的状态必须用bitmask,作为一个特例,由于Running状态的bit码是0,因此,需要用如下方式判断线程是否处于运行状态:(myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0。

枚举成员如下:

 

  Member name Description
  Running The thread has been started, it is not blocked, and there is no pendingThreadAbortException.
  StopRequested The thread is being requested to stop. This is for internal use only.
  SuspendRequested The thread is being requested to suspend.
  Background The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting theThread.IsBackground property.
  Unstarted The Thread.Start method has not been invoked on the thread.
  Stopped The thread has stopped.
  WaitSleepJoin The thread is blocked. This could be the result of calling Thread.Sleep orThread.Join, of requesting a lock — for example, by callingMonitor.Enter orMonitor.Wait — or of waiting on a thread synchronization object such asManualResetEvent.
  Suspended The thread has been suspended.
  AbortRequested The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pendingSystem.Threading.ThreadAbortException that will attempt to terminate it.
  Aborted The thread state includes AbortRequested and the thread is now dead, but its state has not yet changed toStopped.

 

状态转换图如下“

\

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,