private async void ShellWindow_OnClosing(object sender, CancelEventArgs e)
{
// While the application is closing using async tasks and the task factory is error prone.
// Must wait for the ShellClosing method to complete before continueing to close.
Controller.SaveApplicationState();
if (Controller.HasUnsavedChanges)
{
e.Cancel = true;
if (await Controller.ShellClosing())
{
try
{
Close();
}
catch (InvalidOperationException)
{
// Continue, this ex is thrown when app is shutting down and user has chosen not to save.
Application.Current.Shutdown();
}
}
}
}
}