Analysis on the Problem of closing the login form and welcome form in c,

Source: Internet
Author: User

Analysis on the Problem of closing the login form and welcome form in c,
In c # winform programming, we often do login forms or welcome forms and use them as startup forms. However, we may encounter some problems. See the following code: private void button#click (object sender, EventArgs e) {this. close (); new Form2 (). show ();} this code will enable "form1" in "form1" to close "form1" and show "form2", but the purpose is not met. "form2" is shown, but it is only a flash, then it disappears with the disabled form1. What's going on? Let's find the answer from the Main method. This is the automatically generated Main method: static void Main () {Application. enableVisualStyles (); // enable the visual style Application. setCompatibleTextRenderingDefault (false); // set UseCompatibleTextRendering of some controls to the default value Application. run (new Form1 (); // This is the key} we can see this sentence Application. run (new Form1 (); what does this mean? This is to run the standard application message loop on the current thread without a form, and display this form. That is to say, to display a form on a thread without a form, so that the thread does not end. Back, In the button1 click event, what is the form2 we show? This form2 live on a new thread, so the two forms will not block each other. So the question is, why should we turn off form1 and form2? The reason is that the thread where form2 is located is the affiliated thread of the show thread. In this case, the form2 thread belongs to the form1 thread. When form1 is disabled, the thread where form1 is located ends. When it is associated, all its affiliated threads are terminated. The thread where form2 is located is no exception, therefore, form2 is also disabled. If you know the reason, let's think about the solution. Application. Run (); it looks very good. Let's try it. The changed button1 code is as follows: private void button#click (object sender, EventArgs e) {this. close (); Application. run (new Form2 ();} but the running result is an error. Why? This is because a form is already running on the thread where form1 is located and a standard application message loop is running. Therefore, a new loop cannot be started on this thread, A new form is displayed. Since this thread cannot have another form, let's try another thread! The final button1 Click Event code is as follows: copy the code private void button#click (object sender, EventArgs e) {this. close (); new System. threading. thread () => {Application. run (new Form2 ());}). start ();}

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.