tags:

views:

1117

answers:

3

I have a WPF window which I am creating from another window, show()ing, then letting it close() itself. When the window closes, I expect it to die, call its constructor, and delete all its child elements (such as timers..).

What is the correct way of invoking this action?

+4  A: 

Deleted previous answer (was a completely wrong guess).

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.

Simon Buchan
The Window class in WPF doesn't have a Dispose() method!
Samuel Jack
Maybe I should read the docs first?
Simon Buchan
+2  A: 

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.

Samuel Jack
My docs say only unmanaged resources are disposed...
Simon Buchan
A: 

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.

Drew Noakes