C # foreground thread and background thread difference and contact (GO)

Source: Internet
Author: User

. The common language runtime (Common Language runtime,clr) of net can differentiate between two different types of threads: foreground and background threads. The difference between the two is that the application must run out of all foreground threads to exit, and for a background thread, the application can exit without considering whether it has finished running, and all background threads will automatically end when the application exits.

A thread established by the. NET environment by using thread by default is a foreground thread, that is, the thread property Isbackground=false, in which the process does not terminate as long as there is a foreground thread that does not exit. The main thread is a foreground threading. The background thread terminates automatically, regardless of whether the thread ends, as long as all foreground threads exit (including normal exit and exception exit). A generic background thread is used to handle short-time tasks, such as using a background thread in a Web server to handle request information sent by the client. The foreground thread is typically used to handle tasks that require long waits, such as a program that listens to client requests in a Web server, or a program that periodically scans certain system resources. The following code shows the difference between foreground and background threads.

 Public Static void Mystaticthreadmethod () {    thread.sleep(  Mystaticthreadmethod); // thread. IsBackground = true; Thread. Start ()

If you run the above code, the program waits 3 seconds to exit, and if you remove the comment and set the thread as a background thread, the program exits immediately. Note that the type of the thread must be set before the Start method is called, otherwise the type of the thread cannot be changed if it is run.

The thread that runs through the BeginXxx method is a background thread, a program that starts multiple threads has a problem when it shuts down, and if the program exits without closing the thread, the thread will always exist, but most of the threads that are started are local variables and cannot be shut down. If the Thread.CurrentThread.Abort () method is called to close the main thread, a ThreadAbortException exception occurs, so this is not possible. Later found this method: Thread.IsBackground Set the thread as a background thread.

Transfer from http://www.cnblogs.com/tianzhiliang/archive/2011/03/03/1970042.html

C # foreground thread and background thread difference and contact (GO)

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.