I had issue today and while trying to solve it found something strange in WPF.
To see problem, create new WPF application, add button and click event:
private void Button_Click(object sender, RoutedEventArgs e)
{
var window = new MainWindow();
}
Starting application and closing it without clicking button will work.
Starting application, clicking button and closing it - will not work.
Application will stay in memory. Some months ago I had similar issue, but if I use solution from it, then another weird thing occurs: when exiting application, Window.Closed
event is fired for a never displayed window.
Questions: What does WPF do when instantiating window? Why I can't simply instantiate it like any other class? Must I always display it?