0

I have login window. From this login window, i am intializing the main window. Once login successfully happened, i close the login window. Now i am having two other windows, which i am calling from Main Window. Once i close the main Window, I am able to close the other two windows as well as Main Window. But program still runs in memory. I have to close it manually from the Visual Studio. How should i close the Program all instances fully?? This is the Main window Close Event code.

private void usimClose(object sender, EventArgs e)
{
   newScreen2.Close();
   newScreen3.Close();  
   this.Close();                        
}

This is my Login Window Code. Once the user click on the submit button.

 private void btnLogin_Click(object sender, RoutedEventArgs e)
 {
        if (txtUserName.Text.Length == 0)
        {
            errormessage.Text = "Please Enter UserName";
            txtUserName.Focus();
        }
        else
        {                
            LoginStatus _status = _Login.LoginUsimgClient(txtUserName.Text, txtPassword.Password.ToString());

            if (_status.BoolLoginstatus)
            {
                mainWindow.RunApplication();
                string struserName = _status.StringUserFirstName;
                mainWindow.userName.Text = "Welcome " + struserName;
                mainWindow.Show();
                this.Close();                    
            }
            else
            {
                errormessage.Text = _status.StringErrorDescription;
                txtUserName.Text = String.Empty;
                txtPassword.Password = String.Empty;
            }
        }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Maverick
  • 1,952
  • 4
  • 21
  • 33
  • i think the mainWindow is a property of your login window, so it holds the login window in memory – punker76 Jul 19 '13 at 07:08
  • yes. i think so. How should i resolve it then? – Maverick Jul 19 '13 at 07:14
  • instead of closing it, if i hide my login window this.Visibility = System.Windows.Visibility.Hidden; But from Main Window how should i close the Login Window? – Maverick Jul 19 '13 at 07:15
  • try to show and handle your login stuff at the OnStartup event of the App – punker76 Jul 19 '13 at 07:19
  • If I understand that your main window is member of login window, then that is not correct. You should create your login window in your App class on startup event and when login functionality is done instantiate main window and show it. Other windows modal/not should have main window as parent so that they are collected when main window is closed. – Bhupendra Jul 19 '13 at 07:37
  • see this if it works for you http://stackoverflow.com/questions/6982116/wpf-closing-all-windows-when-user-closes-one-of-them – Bhupendra Jul 19 '13 at 07:37
  • There were some ForeGroundThread running in other window. I closed them. Now i am able to close all windows. Thanks – Maverick Jul 24 '13 at 14:58

4 Answers4

7

Try Application.Current.Shutdown();

From MSDN

Calling Shutdown explicitly causes an application to shut down, regardless of the ShutdownMode setting. However, if ShutdownMode is set to OnExplicitShutdown, you must call Shutdown to shut down an application.

Important note

When Shutdown is called, the application will shut down irrespective of whether the Closing event of any open windows is canceled.

This method can be called only from the thread that created the Application object.

Community
  • 1
  • 1
Koen
  • 2,501
  • 1
  • 32
  • 43
4

You can close all windows using this

App.Current.Shutdown();

or

you can manually close it

Window parentwin = Window.GetWindow();
parentwin.Close();
Ankit Jain
  • 1,226
  • 1
  • 10
  • 17
  • There were some ForeGroundThread running in other window. I closed them. Now i am able to close all windows. Thanks – Maverick Jul 24 '13 at 14:57
2

If you starting point is your MainWindow, then just start there.

Firstly, host the LoginForm in your MainWindow, and show it using ShowDialog() to force the user to interact with the LoginForm. Return the result of a successful/unsuccessful interaction to the MainForm.

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        var form = new LoginForm();
        var result = form.ShowDialog();

        if (result ?? false)
        {
            // Carry on opening up other windows
        }
        else
        {
            // Show some kind of error message to the user and shut down
        }
    }

Otherwise, technically your LoginForm is hosting your MainForm which is, frankly, odd.

chris.ellis
  • 883
  • 6
  • 10
1

Have a look at my answer here: How to close wpf window from another project

An Application.Current.Shutdown() will stop the application in a very abrupt way.

It is better to gracefully keep track of the windows and close them.

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115