When working together, create a U-level, log in from the login form successfully, then go to the main form and hide the login form (because the login form is close () and the entire program ends). However, when the main form is closed, the program running in VS is not finished and is still running. This means that only the thread of the main form is closed, and the thread of the login form is not closed, because we just hid it before and did not close it directly. This is when the user closes the application and the desktop does not appear, but the thread that is logged on to the form is still running. Feeling like this is always bad, so long to solve it.
The original program code was written like this.
Program
<span style= "FONT-SIZE:18PX;" > Static Class Program {///<summary>// The main entry point of the application. // </summary> [STAThread] static void Main () { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (New Frmlogin ()); } } </span>
Frmlogin
<span style= "FONT-SIZE:18PX;" >//<summary>//Login//</summary>// <param name= "Sender" ></param> //<param name= "E" ></param> private void Btnok_click (object sender, EventArgs e) { Frmmain frmmain = new Frmmain (); Frmmain.show (); This. Hide (); } </span>
After surfing the internet, the code is now written like this.
Program
<span style= "FONT-SIZE:18PX;" > static void Main () { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Instantiate login form frmlogin login = new Frmlogin (); If you return to the login successfully, instantiate the main form if (login.showdialog () = = DialogResult.OK) { Application.Run (new Frmmain ()); } }</span>
Frmlogin
<span style= "FONT-SIZE:18PX;" >//<summary>//Login//</summary>// <param name= "Sender" ></param> //<param name= "E" ></param> private void Btnok_click (object sender, EventArgs e) { // If the login succeeds, DialogResult becomes OK this . DialogResult = DialogResult.OK; Close this form . Close (); } </span>
Program run time is no longer directly inside Application.Run (new Frmlogin), but the first new one frmlogin, and so on to determine the success of the login , Then Application.Run in the program (New Frmmain ()) and then closes the frmlogin instead of hiding it. So in the current program only frmmain this form is running, there will be no frmmain form closed, and the login form thread is still in the situation.
Note: For the difference between show () and ShowDialog (), you can refer to the blog http://blog.csdn.net/zlwzlwzlw/article/details/7915862
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Login form still runs after main form is closed