Reputation: 1350
I have a fairly WPF application with a strange behavior: after I close it when debugging, the window gets closed but the debug (so the app itself) is still running. I'm talking about clicking the close button on the window - forcing shutdown by clicking Stop Debugging works.
I guess some background thread is still running even when the UI thread finished. I don't have any multi-threaded code using the Thread
class, however I'm using a library called Ookii.Dialogs
which contains a ProgressWindow
class that is used much like the good old BackgroundWorker
. This class is my primary suspect, however I believe I'm using it correctly - just handling long-running operations in the DoWork
handler, showing progress by calling ReportProgress
, and starting them with the Show()
method, which is something like RunWorkerAsync
, but it also displays a progress window. When the operation finished, the window is automatically closed.
Let's say it's not the library's fault and replacing it with a BackgroundWorker
(which would be the same, only without the progress window) would still show the same symptoms. Any idea how I could begin to diagnose this?
Upvotes: 2
Views: 282
Reputation: 1350
Found it! The problem was I created a window instance that was never showed. This is what I had:
private CustomWindow myWindow;
public MainWindow() {
this.myWindow = new CustomWindow();
}
public void Button_Click(object sender, RoutedEventArgs e) {
this.myWindow.Show();
}
Even if I never clicked the button (so the window was never showed), a Window instance was still created and left waiting in memory. This prevented the app from shutting down.
Changing the code like this fixed the problem:
private CustomWindow myWindow;
public MainWindow() {
}
public void Button_Click(object sender, RoutedEventArgs e) {
this.myWindow = new CustomWindow();
this.myWindow.Show();
}
The window is now created only at the point when it needs to be displayed (which is a better way to go anyway), and when it is displayed, it can be actually properly closed - unlike in the first case, when not clicking the button left the window in a weird, "created but never actually displayed nor closed" state, which prevented the app from shutting down, since, as crashmstr pointed out, my App had a default ShutdownMode of OnLastWindowClose.
Upvotes: 1
Reputation: 28573
Not sure if you have the same problem, but the same kind of this happened to us.
In your Application
class, try setting ShutdownMode = ShutdownMode.OnMainWindowClose
. the default is last window (even though there were no windows open, this was still a problem).
MSDN Documentation for Shutdownmode
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs args)
{
///Set this to OnMainWindowClose (defaults to OnLastWindowClose)
///This seems to prevent the app from running after the main window closes
///--alternate solution would be to call Application.Current.Shutdown() from MainWindow.OnClosing
ShutdownMode = ShutdownMode.OnMainWindowClose;
}
}
Upvotes: 1
Reputation: 50682
Attach Visual Studio to the running process and pause it.
Have a look at the threads and what it is they are doing.
Upvotes: 1