How to determine the current state of a thread in C #

Source: Internet
Author: User

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.