The difference between main thread and sub thread

The difference between main thread and sub thread
Each thread has a unique identifier to distinguish the primary and secondary relationships in the thread.  
Thread unique identifier: Thread.CurrentThread.ManagedThreadID;

Both UI interface and Main function are Main threads.
The method body or delegate contained by the Thread is a child Thread.
A delegate can contain multiple method bodies, which can be executed with this.Invoke.
You can also define a variety of method bodies to execute in Thread. The method bodies are all child threads. Note that if you want to modify the display of the UI interface. You need to use this.Invoke, otherwise an exception will be reported.
The Main function is the Main thread, and the id identifier is the same as the UI Main thread

Not much. See the following test code comments.
--------------------- 
Author: autumn wind ten li
Source: CSDN
Original: https://blog.csdn.net/qq_/article/details/51850264
Copyright notice: This is the original article of the blogger. Please attach the link of the blog!

public delegate void dele();
    public partial class Form1 : Form
    {
        int idA;
        public dele getlab;
        public Form1()
        {
            InitializeComponent();
            idA  = Thread.CurrentThread.ManagedThreadId;//UI main thread
            /*
             * Main The function is the main thread, and the id identifier is the same as the main thread of UI interface.
             */
        }

        Thread thread;
        private void getbtu_Click(object sender, EventArgs e)//UI control belongs to main thread
        {
            int idB = Thread.CurrentThread.ManagedThreadId;
            getlab = new dele(getLabel);//Delegate add method
            getlab += new dele(gg);//Delegate is superimposing a method
            getlab += new dele(kk);//Delegate is superimposing a method
            //
            thread = new Thread(new ThreadStart(delegate
            {
                int idC = Thread.CurrentThread.ManagedThreadId;//Thread opens up a sub thread
                //gg(); / / methods are called directly in the thread, and all of them are sub threads
                //kk(); / / methods are all called as child threads directly in thread
                getlab();//The delegate is called directly in the thread, and the method in the delegate is a child thread

                //All of these are contained in thread
                MessageBox.Show("Display completed");

                //this.Invoke(getlab);
                /*Error tip: the main thread will certainly die if it calls the main thread*/
            }));
            thread.IsBackground = true;
            thread.Start();
        }
        public void kk()//Method in delegation method 3
        {
            int idR = Thread.CurrentThread.ManagedThreadId;
            this.Invoke((dele)(() =>//If you want to modify the UI interface value, you need to add this.Invoke
            {                 
                    label2.Text = "ABC";
            }));
        }
        public void gg()//Method in delegation method 2
        {
            int idP = Thread.CurrentThread.ManagedThreadId;
            this.Invoke((dele)(() =>//If you want to modify the UI interface value, you need to add this.Invoke
            {
                    label3.Text = "QWE";
            }));
        }
        public void getLabel()//Method in delegation method 1
        {       
            int idD = Thread.CurrentThread.ManagedThreadId;//Sub thread

            for (int i = 0; i <= 1000; i++)
            {
                Thread.Sleep(10);
                //this.Invoke is the main thread and executes the contents of the sub thread
                this.Invoke((dele)(() =>
                {
                    getlabel.Text = i.ToString();
                    int idE = Thread.CurrentThread.ManagedThreadId;//Main thread
                    Console.WriteLine("thread idE:" + idE);
                }));
                Console.WriteLine(i);
            }
            MessageBox.Show("Display completed");
        }

        private void sleepbtu_Click(object sender, EventArgs e)//UI control belongs to main thread
        {
            int idF = Thread.CurrentThread.ManagedThreadId;
            try
            {
                if (thread.ThreadState != ThreadState.Suspended)
                {
                    thread.Suspend();
                }
            }
            catch (Exception r)
            {
                MessageBox.Show(r.Message);
            }
        }

        private void setbtu_Click(object sender, EventArgs e)//UI control belongs to main thread
        {
            int idG = Thread.CurrentThread.ManagedThreadId;
            try
            {
                if (thread.ThreadState != ThreadState.Running)
                {
                    thread.Resume();
                }
            }
            catch (Exception r)
            {
                MessageBox.Show(r.Message);
            }
        }

        public void getlabel_Click(object sender, EventArgs e)
        {

        }
    }
--------------------- 
//Original: https://blog.csdn.net/qq_/article/details/51850264 

Conclusion:
1. What are sub threads?  
Included in Thread thread = new Thread(new ThreadStart(delegate {)
})); all of them are regarded as sub threads. (personal understanding)
2. What is the main thread?  
UI interface and Main function are both Main threads, except "programs not included in Thread" can be regarded as Main threads.
Original: https://blog.csdn.net/qq_/article/details/51850264
 

Posted on Sun, 01 Dec 2019 00:51:37 -0800 by dta