How to determine the current state of the thread in C # from the source
In C #, thread object thread uses the ThreadState property to indicate the thread state, which is an enumerated type object with the Flags attribute.
ThreadState defines a thread for threads
a set of all possible execution states。 Once the thread is created, it
in at least one of the statesuntil it terminates. Threads created in the common language runtime are initially in the unstarted state, while external threads that enter the runtime are already in the running state. The unstarted thread can be converted to the running state by calling start. Not all combinations of threadstate values are valid; For example, a thread cannot be in both the aborted and unstarted states. So judging the current state of the thread must use bitmask bitwise operation to achieve the purpose of judgment, can not directly use equality to judge. The definition threadstate is as follows:
1. Determine if the thread is in A canceled state A. Wrong Judgment (mythread.threadstate = = threadstate.abortrequested) B. Correct judgment (myThread.ThreadState &am P threadstate.abortrequested)! = 0 2. Determine if a thread is running this is a bit special, because threadstate.running itself equals 0, cannot use &, so the following methods can be judged: (mythr ead. ThreadState = = threadstate.running)
How to determine the current state of a thread in C #