Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 983
Victor Mukherjee Avatar asked Apr 26 '13 09:04

Victor Mukherjee


People also ask

How do I know if a thread is running C#?

To check the status of the current thread in C#, use the IsAlive property.

What is thread state in C#?

ThreadState. A thread is created within the common language runtime. Unstarted. Another thread calls the Thread. Start method on the new thread, and the call returns.

How do you know when a thread is completed?

Use Thread. Join(TimeSpan. Zero) It will not block the caller and returns a value indicating whether the thread has completed its work.


1 Answers

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

msdn Thread.ThreadState

  • Running
    The thread has been started, it is not blocked, and there is no pending ThreadAbortException.
  • StopRequested
  • SuspendRequested
  • Background
  • Unstarted
  • WaitSleepJoin
  • Suspended
  • AbortRequested

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

like image 109
WhileTrueSleep Avatar answered Sep 21 '22 14:09

WhileTrueSleep