Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

WPF application is not closing correctly

I am calling Application.Current.Shutdown() from a class that is bound to xaml windows with ObjectDataProvider, but the application is not closing. Can anyone help me to understand why? My application is not closing completely after my main window is closed, it doesn't disappear from task manager's process list.

Upvotes: 7

Views: 13577

Answers (6)

Philip
Philip

Reputation: 308

I had a problem where the application would not shut down even when main window was closed. It turned out I had done Hide() on the splash screen instead of Close() so it was still lurking in the background keeping the application alive.

Upvotes: 1

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

Try Environment.Exit(0) instead

Upvotes: 26

Nassim
Nassim

Reputation: 66

I had the same problem, the application process doesn't stop although the application closed.

In my case I opened a window from a BackgroundWorker (code below)

BackgroundWorker BG = new BackgroundWorker();
BG.DoWork += new DoWorkEventHandler(BG_DoWork);
StockMinWindow MinWindow = new StockMinWindow(null); -------- this is the problem 
BG.RunWorkerAsync();

instanciate the window before running the BackgroundWorker seem not being the problem but by erasing the line the application closed correctly

I open my window from the BackgroundWorker but using the principal Thread (code below)

 View.Dispatcher.BeginInvoke(new Action(delegate()
 {
   StockMinWindow MinWindow = new StockMinWindow(StockMinList);
   MinWindow.Owner = View;
   MinWindow.ShowDialog();
 }));

Hope it helps.

Upvotes: 0

Steven Richards
Steven Richards

Reputation: 2822

If you have multiple windows or dialogs in your application, you may need to close each one explicitly.

Close dialogs with:

_myDialog.Close();

Close all windows:

foreach(var window in Application.Current.Windows.ToList())
{
    window.Close();
}

Upvotes: 1

Bill Kerr
Bill Kerr

Reputation:

Don't forget to add this:

private void Window_Closed(object sender, EventArgs e)
{
  Application.Current.Shutdown();
}

Hope this helps.

Upvotes: 5

Orion Edwards
Orion Edwards

Reputation: 123612

Have you created any threads to do background processing? If you have, make sure to set the .IsBackground property on them, or they can keep the app running

Upvotes: 11

Related Questions