The first time in the Cnbogs article, this time to a very basic, mainly to small white look.
In C # WinForm programming, we often do login forms or welcome forms and use them as startup forms.
However, we may encounter some problems.
Take a look at the following code:
Private void button1_click (object sender, EventArgs e) { this. Close (); New Form2 (). Show (); }
This code wants to let Form1 in Button1 after the click to close form1,show out Form2, but the goal did not reach, Form2 is show out, but just a flash, then and closed Form1 together disappeared.
What's going on here? Let's find the answer from the main method. This is the auto-generated Main method:
Static void Main () { application.enablevisualstyles (); // Enable visual styles Application.setcompatibletextrenderingdefault (false); // set the usecompatibletextrendering of some controls to their default values Application.Run (new Form1 ()); // This is the key }
We see this sentence
Application.Run (new Form1 ());
What does that mean? This is to start running a standard application message loop on the current thread without a form, and display the form.
That is, to display a form on a thread that does not have a form, so that the thread does not end.
Come back, in the Button1 click event, we show out of the Form2 is what situation? This Form2 is alive on a new thread, so the two forms don't block each other.
So the question comes, why turn off the Form1, and the Form2 will turn off?
The reason is that the thread that Form2 is on is the satellite thread that is show its thread, and here is the thread that the Form2 thread belongs to Form1. When the Form1 is closed, the thread of the Form1 is ended, along with it, all of his subordinate threads are ended, and Form2 's thread is naturally no exception, so Form2 will also be closed.
Knowing the reason, then we come to think about the solution. Application.Run (); it looks like a bull, let's try it.
The changed Button1 code is as follows:
Private void button1_click (object sender, EventArgs e) { this. Close (); Application.Run (new Form2 ()); }
But the result of the operation is an error
What's going on here? This is because a form is already running on the thread where the Form1 is running, and the standard application message loop runs, so it is not possible to open a new loop on this thread and display a new form.
Since this thread can't come up with a form anymore, let's try a different thread! The final Button1 Click event code is as follows
Private void button1_click (object sender, EventArgs e) { this. Close (); New System.Threading.Thread (() = { Application.Run (new Form2 ()); }). Start (); }
Here, I created a new thread, and in this line thread run Form2, opened the new application loop, as we expected, successfully run, form2show out, Form1 off.
To summarize:
A thread can only run a form,
Application.Run () can start running a standard application message loop on the current thread .
To open a form that is not affected by the original form,
A new thread needs to be opened.
In the new thread,
Use
Application.Run ()
This is useful in login forms and welcome forms
An analysis of the sign-in form and welcome form shutdown in C #