Multi-thread non-main thread destruction mechanism, multi-thread destruction

Source: Internet
Author: User

Multi-thread non-main thread destruction mechanism, multi-thread destruction

Suppose there are multiple threads as shown in the following code:

1 private void btnNewThread_Click (object sender, EventArgs e) 2 {3 Thread t = new Thread (new ThreadStart (Test); 4 t. start (); 5 this. btnNewThread. enabled = false; 6} 7 private void Test () 8 {9 //...... 10}

Will the newly created thread t be automatically destroyed after the Test () method is executed? Or do I need to write code to manually destroy it?

The following is a summary of the multi-threaded non-main thread destruction mechanism:

1 ).T will be automatically destroyed after it ends.

2 ).Set thread attribute IsBackground = true

The thread t is used as the background thread and ends with the main thread, regardless of whether the thread has been executed.

3 ).Manual destruction

You can add a thread name when creating a thread, store it in the thread pool, and find the corresponding thread to be destroyed from the thread pool when the thread is to be destroyed.

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.