Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Should I manually call Close on windows opened by ShowDialog method?

The thing is that WPF Window doesn't implement IDisposable interface which led me to believe, that I don't have to manually dispose of it when I open it by calling ShowDialog() but the first comment on this MSDN page states differently. Does anybody know what's the truth?

like image 811
aks Avatar asked Feb 12 '10 12:02

aks


1 Answers

Only if you open the window using Show() instead of ShowDialog().

From the documentation:

If a window, opened by calling ShowDialog, and with a Button with its IsCancel property set to true, will automatically close when the button is either clicked, or ESC is pressed. If the window was opened using Show, however, Close must be explicitly called, such as from Click event handler for the Button.

and:

Closing a window causes the Closing event to be raised. If the Closing event isn't canceled, the following occurs:

  • The Window is removed from Application.Windows (if an Application object exists).
  • 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.
  • The Closed event is raised.
  • Unmanaged resources created by the Window are disposed.
  • If ShowDialog was called to show the Window, ShowDialog returns.
like image 154
Andy Shellam Avatar answered Sep 29 '22 08:09

Andy Shellam