What is the correct way to dispose of a WPF window?


Wpf Problem Overview

I have a WPF window which I am creating from another window by calling Show(), then letting it Close() itself. When the window closes, I expect it to die, call its destructor, and delete all its child elements (such as timers..).

What is the correct way of invoking such an action?

Wpf Solutions

Solution 1 - Wpf

Close() releases all unmanaged resources, and closes all owned Windows.

Any other managed resources you need deterministic disposal of should be handled from the Closed event.


(note: deleted previous answer, it was a completely wrong guess)

Solution 2 - Wpf

There are very few WPF elements that actually need to be explicitly disposed, unlike in Windows Forms.

In the case of Window, calling Close() is sufficient to dispose all managed and unmanaged resources accorrding to the documentation.

Solution 3 - Wpf

Just in case, I'll add my two cents.

My problem was that I didn't do enough troubleshooting. My window was a child window that could be opened, closed, and re-opened, so I added the following to keep it from closing completely:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
  e.Cancel = true;

However, when Window.Close was called, it only hid the window. I eventually caught on and added the following:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
  e.Cancel = true;

public void Close() {
  this.Closing -= Window_Closing;
  //Add closing logic here.

This works fine - it removes the event handler preventing the window from being closed, and then closes it.

Solution 4 - Wpf

Closing the window and being confident that you have released all resources to it and any of its children will cause all well behaved elements in the logic tree to be garbage collected.

I say "well behaved" because it's theoretically possible to have an element that does something like create a thread that isn't stopped properly, but in practice if you're using the basic WPF framework and well written controls, you should be fine to just release everything.

Solution 5 - Wpf

Regarding resources held by the Window, according to the documentation:

(emphasis added)

> Closing a window causes the Closing event to be raised. If the Closing > event isn't canceled, the following occurs: > > 1. The Window is removed from Application.Windows (if an Application > object exists). >
> 2. The Window is removed from the owner Window if the owner/owned > relationship was established before the owned Window was shown and > after the owner Window was opened. >
> 3. The Closed event is raised. >
> 4. Unmanaged resources created by the Window are disposed. >
> 5. If ShowDialog was called to show the Window, ShowDialog returns.

I believe this is listed in sequential order.

I'm not sure specifically about the timers sub-question; I don't think the question has enough detail to really answer that.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionvanja.View Question on Stackoverflow
Solution 1 - WpfSimon BuchanView Answer on Stackoverflow
Solution 2 - WpfSamuel JackView Answer on Stackoverflow
Solution 3 - Wpfrookie1024View Answer on Stackoverflow
Solution 4 - WpfDrew NoakesView Answer on Stackoverflow
Solution 5 - WpfStayOnTargetView Answer on Stackoverflow