C# Thread.IsAlive 和 Thread.ThreadState==ThreadState.Running

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16232521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:28:11  来源:igfitidea点击:

Thread.IsAlive and Thread.ThreadState==ThreadState.Running

c#multithreadingthread-state

提问by Victor Mukherjee

I am using to check the condition of a thread with if(Thread.IsAlive). A form is running in this thread. At times during execution, even though the form remains open, the call to Thread.IsAlive seems to be evaluating to false. I thought to perform the same check with if(Thread.ThreadState==ThreadState.Running). Is it the right way to do? If not, what is the possible work around?

我正在使用if(Thread.IsAlive). 一个表单正在这个线程中运行。有时在执行过程中,即使表单保持打开状态,对 Thread.IsAlive 的调用似乎评估为 false。我想用if(Thread.ThreadState==ThreadState.Running). 这是正确的做法吗?如果没有,可能的解决方法是什么?

回答by WhileTrueSleep

msdnThread.IsAlive Property true if this thread has been started and has not terminated normally or aborted; otherwise, false.

msdnThread.IsAlive 属性 如果此线程已启动且未正常终止或中止,则为 true;否则为假。

msdnThread.ThreadState

msdnThread.ThreadState

  • Running
    The thread has been started, it is not blocked, and there is no pending ThreadAbortException.
  • StopRequested
  • SuspendRequested
  • Background
  • Unstarted
  • WaitSleepJoin
  • Suspended
  • AbortRequested
  • Running
    线程已经启动,没有阻塞,也没有挂起的ThreadAbortException。
  • 停止请求
  • 暂停请求
  • 背景
  • 未启动
  • 等待睡眠加入
  • 暂停
  • 中止请求

I think now it's clear Runningis not the same as IsAlive

我想现在很明显Running不一样IsAlive