Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end process after closing whole application in C#.NET?

I have a .NET 2005 (C#) desktop application, in which there is a login form and an MDI form, which then have multiple subforms. All other forms are opened in MDI form only.

After a user logs in, I hide the login form and then show the MDI form, but when I close the MDI form, my application process does not end because the login form is still hidden. I want that, when the user closes the MDI form, the whole application should close (essentially, the process should not be shown in the task manager), because if everytime the user closes and reopens the application and logs in, it will create some performance problem.

I am doing something like below:

//my login validation script,

//after successful login

this.Hide();

if (globalData.ObjMdiMain.IsDisposed)
{
    globalData.ObjMdiMain = new mdiMain();
}
globalData.ObjMdiMain.Show();

globalData is my static class where I create global objects which are required by the whole application. There, I have defined the ObjMdiMain object of my MDI form and I am accessing it here in the login form.

So, is there any method or function which will end the whole process from system, something like "Application.End();" or something else?

Thanks!

like image 875
djmzfKnm Avatar asked Nov 20 '25 02:11

djmzfKnm


2 Answers

I assume from your description (please correct me if I'm wrong), that you have the login form set as your startup form, like this:

    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new LoginForm());
    }

Don't do that. Change it to something like this instead:

    static void Main() {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        LoginForm frm = new LoginForm();
        if(frm.ShowDialog() == DialogResult.Cancel) {
            return;
        }

        Application.Run(new MdiForm());
    }

In your login form code, do not call the MDI form at all, just validate the username and password and allow the login form to exit. The Main method will then call the MDI form properly.

Your application will then exit naturally when you close the MDI form.

like image 119
Christian Hayter Avatar answered Nov 22 '25 16:11

Christian Hayter


I find it strange that you leave the login window hidden after the initial login. Why not close it? Then you would not have your current problem: after successful login you only have the main MDI window that when is closed exits the running Windows Forms application. Is there a reason you hide the login window instead of closing it?

like image 27
peSHIr Avatar answered Nov 22 '25 16:11

peSHIr