C # open a new form in the thread

Source: Internet
Author: User

Recently I made a winform program to avoid applications with multiple forms. I naturally had a lot of questions in it. First, when we click a button in the main form to open a new form, is this new form opened in a new thread? So I made a test code as follows:

Form. CS

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading; namespace test {public partial class form1: FORM {public form2 F2; Public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs E) {int threadid = thread. currentthread. managedthreadid; textbox1.text = threadid. tostring (); // display the main thread ID in the text box F2 = new form2 ();} private void button#click (Object sender, eventargs e) {f2.show (); // display form2 }}}

Form2.cs

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading; namespace test {public partial class form2: FORM {int I = 1; Public form2 () {initializecomponent ();} private void form2_load (Object sender, eventargs e) {MessageBox. show (thread. currentthread. managedthreadid. tostring (); // The current thread is displayed }}}

Running result:

Obviously, they all run in thread 10, but now there is a problem. If, if we read a text file in form1 and perform write operations on this text file in form2, will this conflict? This is to be tested and omitted here.

 

Next, open form2 in the thread.

The form1.cs code is as follows:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading; namespace test {public partial class form1: FORM {public form2 F2; Public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs E) {int threadid = thread. currentthread. managedthreadid; textbox1.text = threadid. tostring (); // display the main thread ID in the text box F2 = new form2 ();} private void button#click (Object sender, eventargs E) {thread thread2 = new thread (threadpro); // create a new thread thread2.start ();} public void threadpro () {f2.show ();}}}

The form2.cs code remains unchanged.

The running result is as follows:

 

We can see that the two forms are now in different threads, but after we click "OK", the form2 form will always flash and disappear. Why? 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.

Modify form1.cs as follows:

 

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading; namespace test {public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs e) {int threadid = thread. currentthread. managedthreadid; textbox1.text = threadid. tostring (); // display the main thread ID in the text box} private void button#click (Object sender, eventargs e) {thread thread2 = new thread (threadpro ); // create a new thread thread2.start ();} public void threadpro () {methodinvoker methinvo = new methodinvoker (showform2); begininvoke (methinvo);} public void showform2 () {form2 F2 = new form2 (); f2.show ();}}}

The form2 code remains unchanged and the running result is as follows:

Click "OK" and the form2 page appears. The problem is that two forms are in one thread. Didn't we create a new thread? What's going on? In my understanding, the methodinvoker in the Code is actually equivalent to a delegate, passing it as a parameter to begininvoke (methinvo); for execution, while the begininvoke method is executed on the control thread, that is, what we usually call the UI thread, that is, our main thread, so it explains why it is 10. What if we want to avoid a thread? I don't know what I can do. Please let me know.

 

We can expand the above program to solve the problem that the sub-thread modifies the control attribute of the main thread. Let's first look at the existence of this problem as follows:

Continue to modify the form1.cs code and display the thread ID in the second text box of form1 as follows:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading; namespace test {public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs e) {int threadid = thread. currentthread. managedthreadid; textbox1.text = threadid. tostring (); // display the main thread ID in the text box} private void button#click (Object sender, eventargs e) {thread thread2 = new thread (threadpro ); // create a new thread thread2.start ();} public void threadpro () {textbox2.text = thread. currentthread. managedthreadid. tostring (); methodinvoker methinvo = new methodinvoker (showform2); begininvoke (methinvo);} public void showform2 () {form2 F2 = new form2 (); f2.show ();}}}

Run the program. Click "1". An error is reported as follows:

It turns out that the window form control cannot be called across threads. It is very simple to set textbox2.text = thread. currentthread. managedthreadid. tostring (); A showform2 () function can solve the problem. This provides an idea that the form attribute can be modified across threads through delegation, I think so. It may be because of my lack of ability that I can only perform this simple analysis, and I want to correct the shortcomings.

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.