How to change the foreground thread to a background thread

Source: Internet
Author: User

Start a number of threads of the program in the shutdown when there is a problem, if the program exits without closing the thread, then the thread will always exist, but most of the start of the thread are local variables, can not be closed, if the call Thread.CurrentThread.Abort () Method closes the main thread, a ThreadAbortException exception appears, so this is not possible.

Later found this approach: Thread.IsBackground set the thread as a background thread.

MSDN explains foreground and background threads: Managed threads or background threads, or foreground threads. Background threads do not make the managed execution environment active, 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 the managed assembly), all background threads are stopped and closed. By setting the Thread.IsBackground property, you can designate a thread as either a background thread or a foreground thread. For example, you can designate a thread as a background thread by setting Thread.IsBackground to True. Similarly, by setting IsBackground to False, you can designate a thread as a foreground thread. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All threads generated by creating and starting a new thread object are foreground threads. If you want to create a foreground thread that you want to use to listen for certain activities, such as a socket connection, you should set the Thread.IsBackground to true so that the process can terminate.

So the solution is to set: Thread.CurrentThread.IsBackground = True when the main thread is initialized;

In this way, the main thread is a background thread, which closes all threads when the main program is closed and the main thread is closed. But in this case, all the threads that are executing are forced to close, so the results of the thread's work are saved at shutdown time.

                                                            --excerpt from the C # programming dictionary

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.