Login form still runs after main form is closed

Source: Internet
Author: User

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

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.