From 155a599878c73ea350c8af6aeb3864eb334eee32 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Tue, 29 Sep 2026 16:07:53 +0200 Subject: [PATCH] App: Add ShutdownNow() and ShutdownGracefully() --- Elwig/App.xaml.cs | 31 ++++++++++++++++----------- Elwig/Windows/AdministrationWindow.cs | 18 +++++++++++----- Elwig/Windows/MainWindow.xaml.cs | 18 ++++++---------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 628b7ce..d633b3a 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -18,12 +18,13 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; +using System.Windows.Input; using System.Windows.Threading; namespace Elwig { public partial class App : Application { - protected static App CurrentApp; + public static App CurrentApp; public static int NumWindows => CurrentApp.Windows.Count; public static bool ForceShutdown { get; set; } = false; @@ -102,7 +103,7 @@ namespace Elwig { InteractionService.ShowException("Fehlerhafte Datenbank", "Schwerwiegender Fehler in der Datenbank, unbedingt den Programmierern melden!", exc); if (InteractionService.AskQuestion("Backup wiederherstellen", "Soll die Datenbank aus einem Backup wiederhergestellt werden?\n\nAchtung: Das kann zu Datenverlust führen!", false)) await BackupService.RestoreDatabase(); - Shutdown(); + ShutdownNow(); return; } @@ -119,7 +120,7 @@ namespace Elwig { InteractionService.ShowException("Fehler", "Fehler beim Laden der Mandantendaten, unbedingt den Programmierern melden!", exc); if (InteractionService.AskQuestion("Backup wiederherstellen", "Soll die Datenbank aus einem Backup wiederhergestellt werden?\n\nAchtung: Das kann zu Datenverlust führen!", false)) await BackupService.RestoreDatabase(); - Shutdown(); + ShutdownNow(); return; } BranchNum = branches.Count; @@ -183,7 +184,7 @@ namespace Elwig { if (Config.Branch != null) { if (!branches.ContainsKey(Config.Branch.ToLower())) { InteractionService.ShowError("Ungültige Zweigstelle", "Ungültige Zweigstelle in Konfigurationsdatei!"); - Shutdown(); + ShutdownNow(); } else { SetBranch(branches[Config.Branch.ToLower()]); } @@ -191,7 +192,7 @@ namespace Elwig { SetBranch(branches.First().Value); } else { InteractionService.ShowError("Ungültige Zweigstelle", "Erkennen der lokalen Zweigstelle nicht möglich!"); - Shutdown(); + ShutdownNow(); } if (Config.WeighingMode == null) { @@ -210,21 +211,27 @@ namespace Elwig { window.Show(); } - public async Task OnClosing() { + public static void ShutdownNow() { + ForceShutdown = true; + Current.Shutdown(); + } + + public async void ShutdownGracefully() { + Mouse.OverrideCursor = Cursors.AppStarting; + try { await BackupService.TryDatabaseBackup(); } catch (Exception exc) { InteractionService.ShowException("Automatisches Backup", exc); } - } - protected override void OnExit(ExitEventArgs evt) { SerialPortWatcher.Dispose(); foreach (var s in EventScales) { s.Dispose(); } Pdf.Cleanup(); - base.OnExit(evt); + + Shutdown(); } public static void SetBranch(Branch b) { @@ -335,8 +342,7 @@ namespace Elwig { await MainDispatcher.BeginInvoke(() => { var d = new UpdateDialog(latest.Value.Version, latest.Value.Url, latest.Value.Size); if (d.ShowDialog() == true) { - ForceShutdown = true; - Current.Shutdown(); + ShutdownNow(); } }); } else if (showAlert) { @@ -354,8 +360,7 @@ namespace Elwig { await Database.Import(filename); }); InteractionService.ShowInformation("Datenbank ersetzen", "Das Ersetzen war erfolgreich!\n\nBitte starten Sie Elwig neu!"); - ForceShutdown = true; - Current.Shutdown(); + ShutdownNow(); } catch (Exception exc) { InteractionService.ShowException("Datenbank ersetzen", "Fehler beim Ersetzen", exc); } diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs index 9bba606..ad09b44 100644 --- a/Elwig/Windows/AdministrationWindow.cs +++ b/Elwig/Windows/AdministrationWindow.cs @@ -108,12 +108,20 @@ namespace Elwig.Windows { LockInputs(); } + public void TryClose() { + var evt = new CancelEventArgs(); + OnClosing(this, evt); + if (!evt.Cancel) Close(); + } + private void OnClosing(object? sender, CancelEventArgs evt) { - if ((IsCreating || IsEditing) && HasChanged) { - if (!InteractionService.AskConfirmation("Schließen bestätigen", "Soll das Fenster wirklich geschlossen werden?")) { - evt.Cancel = true; - return; - } + if (!IsClosing && (IsCreating || IsEditing) && HasChanged) { + try { + if (!InteractionService.AskConfirmation("Schließen bestätigen", "Soll das Fenster wirklich geschlossen werden?")) { + evt.Cancel = true; + return; + } + } catch { } } IsClosing = true; } diff --git a/Elwig/Windows/MainWindow.xaml.cs b/Elwig/Windows/MainWindow.xaml.cs index 070a985..7dabc3a 100644 --- a/Elwig/Windows/MainWindow.xaml.cs +++ b/Elwig/Windows/MainWindow.xaml.cs @@ -50,27 +50,23 @@ namespace Elwig.Windows { _syncTimer.Start(); } - private async void Window_Closing(object sender, CancelEventArgs evt) { - evt.Cancel = !App.ForceShutdown; - + private void Window_Closing(object sender, CancelEventArgs evt) { if (App.NumWindows > 1 && !App.ForceShutdown) { - foreach (var w in Application.Current.Windows.Cast().Where(w => ((w as AdministrationWindow)?.IsEditing ?? false) || ((w as AdministrationWindow)?.IsCreating ?? false))) { + foreach (var w in Application.Current.Windows.Cast().Where(w => w is AdministrationWindow adm && (adm.IsEditing || adm.IsCreating))) { try { w.Close(); } catch { } } Thread.Sleep(100); - if (App.NumWindows > 1 && !Application.Current.Windows.Cast().Any(w => ((w as AdministrationWindow)?.IsEditing ?? false) || ((w as AdministrationWindow)?.IsCreating ?? false))) { - if (!InteractionService.AskConfirmation("Elwig beenden", "Es sind noch weitere Fenster geöffnet.\nSollen alle Fenster geschlossen werden?")) { + if (App.NumWindows > 1) { + var areAdminWindowsOpen = Application.Current.Windows.Cast().Any(w => w is AdministrationWindow adm && (adm.IsEditing || adm.IsCreating)); + if (areAdminWindowsOpen && !InteractionService.AskConfirmation("Elwig beenden", "Es sind noch weitere Fenster geöffnet.\nSollen alle Fenster geschlossen werden?")) { + evt.Cancel = true; return; } - App.ForceShutdown = true; } } - - Mouse.OverrideCursor = Cursors.AppStarting; - await (Application.Current as App)!.OnClosing(); - Application.Current.Shutdown(); + App.CurrentApp.ShutdownGracefully(); } private void Menu_Help_About_Click(object sender, RoutedEventArgs evt) {