private async void MainWindow_Closing(object sender, CancelEventArgs e) { if (ShouldClose == false) { e.Cancel = true; //stop the window from closing. if (PuzzleSavingState == PuzzleSavingStates.SAVING) { ShouldClose = (await this.ShowMessageAsync("Puzzle currently saving", "The Puzzle is currently saving. Do you want to close anyway?\nThe File will probably be corrupted!!!", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "Close", NegativeButtonText = "Cancel", DialogResultOnCancel = MessageDialogResult.Canceled }) == MessageDialogResult.Affirmative); } else if (PuzzleHandle != null && PuzzleHandle.IsSolverRunning) { ShouldClose = (await this.ShowMessageAsync("Puzzle solver running", "The Puzzle solver is currently running. Do you want to close anyway?", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "Close", NegativeButtonText = "Cancel", DialogResultOnCancel = MessageDialogResult.Canceled }) == MessageDialogResult.Affirmative); } else if (PuzzleSavingState == PuzzleSavingStates.NEW_UNSAVED && PuzzleHandle != null && PuzzleHandle?.CurrentSolverState != PuzzleSolverState.UNSOLVED && PuzzleHandle?.CurrentSolverState != PuzzleSolverState.ERROR) { ShouldClose = (await this.ShowMessageAsync("Puzzle not saved yet", "The Puzzle wasn't saved yet. Do you want to close anyway?", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "Close", NegativeButtonText = "Cancel", DialogResultOnCancel = MessageDialogResult.Canceled }) == MessageDialogResult.Affirmative); } else { ShouldClose = true; } if (ShouldClose) // Save all application settings and shutdown the application { PluginFactory.SavePluginSettings(); Properties.Settings.Default.Save(); Application.Current.Shutdown(); } } }