Makach
Makach

Reputation: 7569

How to close a window in c#

I've got the following code in a window B that is started in its own thread from a window A.

view.Closing += (sender, e) =>
{
    view.Visibility = Visibility.Collapsed;
    e.Cancel = true;
};

When I close window A window B remains in memory and the application doesn't dispose. How should I go forward make sure that the application is shut down when closing window A.

edit: the window B takes a while to load and build that's why the code is there.

Upvotes: 1

Views: 2025

Answers (2)

Ruel
Ruel

Reputation: 15780

use Application.Exit();

For WPF: Application.Current.Shutdown();

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273189

Basic solution: Window A needs to keep a ref to Window B and Dispose() it.

You may have to make the Cancel logic in B conditional.

Upvotes: 2

Related Questions