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.