How to close all windows in WPF app?

sewar t 1 Reputation point
2022-12-20T14:29:18.12+00:00

Hii,

After I run the tests , it doesn't close the application window and I don't know what's wrong with the code.

public void Cleanup()
{

        // Close the desktopSession  
        if (DesktopSession != null)  
        {  
            DesktopSession.Close();  
            DesktopSession.Quit();                  
        }  
          
        // Close the session  
        if (AppSession != null)  
        {  
            AppSession.Close();  
            AppSession.Quit();                
        }                           
    }
Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2022-12-20T16:11:28.16+00:00

    It sounds like you're talking about Selenium here and not WPF. Selenium has its own support forum so really you should go there to get help. They can provide better assistance.

    But as a starter the information I see says to call Quit on the session. If you do that then it is supposed to close all the windows it opened. However searching their issue list here seems to indicate there may be some issues with this unless you're doing things the way they expect. I don't use the driver myself so I cannot confirm.

    0 comments No comments

  2. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-12-21T02:04:34.473+00:00

    Here is the method to close all Window of wpf project.

    The proper way to shutdown a WPF app is to use Application.Current.Shutdown() . This will close all open Windows, raise some events so that cleanup code can be run, and it can't be canceled. Environment.Exit() terminates the application immediately even if other threads are executing.

    You should also consider setting the Owner on non-main Windows. The behavior is probably more like what you'd expect from Z-order, minimize and maximize. As an added bonus, the owning window will automatically close when the owner window is closed.

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.