简体   繁体   中英

WPF close all window from the main window

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;
            }
        }
}

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.

You can close all windows using this

App.Current.Shutdown();

or

you can manually close it

Window parentwin = Window.GetWindow();
parentwin.Close();

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM