BudgetAnalyser.ShellWindow.ShellWindow_OnClosing C# (CSharp) Method

ShellWindow_OnClosing() private method

private ShellWindow_OnClosing ( object sender, CancelEventArgs e ) : void
sender object
e CancelEventArgs
return void
        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();
                    }
                }
            }
        }
    }