Background and foreground threads

Source: Internet
Author: User

Excerpt from MSDN

A managed thread is either a background thread, or a foreground thread. The background thread does not leave the managed execution environment running, except that the background thread is the same as the foreground thread. Once all foreground threads are stopped in the managed process (where the. exe file is a managed assembly), the system stops all background threads and shuts down.

Attention

When the runtime stops a background thread because the process is closed, an exception is not thrown in the thread. However, when a thread is stopped because the System.AppDomain.Unload (System.AppDomain) method unloads the application domain, ThreadAbortException is raised both in the background and in the foreground thread.

Use the Thread.IsBackground property to determine whether the thread is a background thread or a foreground thread, or to change its state. You can change a thread to a background thread at any time by setting its IsBackground property to True.

Points

The foreground or background state of the thread does not affect the results of unhandled exceptions in the thread. In the. NET Framework version 2.0, unhandled exceptions in the foreground or background thread will cause the application to terminate. See Exceptions in Managed threads.

A thread that belongs to the managed thread pool (that is, a thread whose Isthreadpoolthread property is true) is a background thread. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All the lines that are generated by creating and starting a new thread object Cheng Tume think of the foreground thread.

If you are using a thread to monitor activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent the process from terminating.

This means that once the main thread of the program is closed (the main thread code execution completes, the program shuts down), the background thread does not force the shutdown and does not throw an exception. So if there is a background thread running, make sure that the foreground thread does not shut down.

Background and foreground threads

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.