How to Exit a Wpf Application Programmatically

How do I exit a WPF application programmatically?

To exit your application you can call

System.Windows.Application.Current.Shutdown();

As described in the documentation to the Application.Shutdown method you can also modify the shutdown behavior of your application by specifying a ShutdownMode:

Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations:

  • When ShutdownMode is set to OnLastWindowClose.
  • When the ShutdownMode is set to OnMainWindowClose.
  • When a user ends a session and the SessionEnding event is either unhandled, or handled without cancellation.

Please also note that Application.Current.Shutdown(); may only be called from the thread that created the Application object, i.e. normally the main thread.

what is the best practice to exit an WPF application?

In your WPF application whenever your MainWindow that is specified as StartupURI in App.xaml is closed your application exits automatically.

Still if you want to handle this exit of application on your end you can go for below solution.

Override the onClosing of MainWindow and manually exit/shutdown your application.

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
// Shutdown the application.
Application.Current.Shutdown();
// OR You can Also go for below logic
// Environment.Exit(0);
}

How to Handle Application Exit/Close

Is calling Application.Current.Shutdown() the proper way to close the application in the MainWIndowViewModel?

Essentially, yes, but I'd hide it behind some IShutdownService so I can mock it more easily when writing the tests for MainWindowViewModel.

How do do handle the OnExit case? How do you call back into the MainWidowViewModel when this happens?

You can use EventToCommand or InvokeCommandAction to call a command when the Closing and/or Closed events of the MainWindow happen.

WPF: Closing a window programmatically without triggering Window_Closing()

I would introduce an interface for the AuxWindows for handling this. (Why an interface? Just to ensure there's no tight coupling between the views and the view models, see MVVM and SOLID.)

interface ICloseableView
{
void Close(bool confirmClose);
}

Then I would implement this interface in my views:

class AuxWindow : Window, ICloseableView
{
private bool confirmClose = true;

public void Close(bool confirmClose)
{
try
{
this.confirmClose = confirmClose;
Close();
}
finally
{
this.confirmClose = true;
}
}

private void Window_Closing(object sender, CancelEventArgs e)
{
if (!this.confirmClose)
{
e.Cancel = false;
return;
}

// Your code here...
}

}

Finally, in the view models of the AuxWindow, I would use this method obtaining it via the interface reference. Remember: a view model should not be aware of the concrete view implementation, so it's a bad practice to provide a view model with a AuxWindow reference, but it's perfectly OK to give it an IClosableView reference:

class AuxWindowViewModel : INotifyPropertyChanged
{
private readonly IClosableView view;

// E.g. use a dependency injection via constructor
public AuxWindowViewModel(IClosableView view)
{
this.view = view;
}

void CloseViewWithoutConfirmation()
{
this.view.Close(false);
}
}

Using this approach, the user will always get a confirmation dialog whenever they close the AuxWindow in a "normal" way via the GUI or a keyboard shortcut. But closing the view from the view model won't show that dialog.

How to exit a wpf application when Application.Current is NULL?

System.Diagnostics.Process.GetCurrentProcess().Kill();

Anywhere and anytime this terminates your application immediately.

how to capture exit of WPF application

You've got two events that may be of interest Closing and Closed.

Closing

Occurs directly after Close is called, and can be handled to cancel window closure.

and Closed

Occurs when the window is about to close.

You'd use them like this

<Window ... Closing="Window_Closing" Closed="Window_Closed">
...
</Window>

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
...
}

private void Window_Closed(object sender, EventArgs e)
{
....
}

As this post says you can still trap the Closing event and handle it from there.

Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()

Application.Current.Shutdown() is the proper way to shutdown an application. Generally, because it fires the exit events, that you can handle, further info.

Process.GetCurrentProcess().Kill() should be used when you want to kill the application as soon as possible, further info.

The nature of those methods is totally different:

  • Shutdown() can be paused to end some operations
  • Kill() forces the application to close immediately, as soon as possible

Probably, Kill() will be the fastest way, but this is something like kernel panic.



Related Topics



Leave a reply



Submit