In multiple threads, some results must be displayed at the end of a thread. I use a new form to display the results. But simply write the following in the thread body:
1 private void ThreadFunc()
2 {
3 MsgForm msg = new MsgForm();
4 msg.Show();
5 }
6 private void button1_Click(object sender, System.EventArgs e)
7 {
8 FormThread = new Thread(new ThreadStart(ThreadFunc));
9 FormThread.Start();
10 }
The generated form is lost in a flash. This is because all the resources in the form created in the thread belong to this thread, so when this thread ends, its resources are also recycled, of course, C # automatically closes the form.
The correct method is to use Invoke. The Code is as follows:
1 private void ThreadFunc()
2 {
3 MethodInvoker mi = new MethodInvoker(this.ShowMsgForm);
4 this.BeginInvoke(mi);
5 }
6 private void ShowMsgForm()
7 {
8 MsgForm msg = new MsgForm();
9 msg.Show();
10 }
11 private void button1_Click(object sender, System.EventArgs e)
12 {
13 FormThread = new Thread(new ThreadStart(ThreadFunc));
14 FormThread.Start();
15 }