App: Add ShutdownNow() and ShutdownGracefully()

This commit is contained in:
2026-09-29 19:04:12 +02:00
parent fda337c3cb
commit 155a599878
3 changed files with 38 additions and 29 deletions
+18 -13
View File
@@ -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);
}
+9 -1
View File
@@ -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 (!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;
}
+7 -11
View File
@@ -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<Window>().Where(w => ((w as AdministrationWindow)?.IsEditing ?? false) || ((w as AdministrationWindow)?.IsCreating ?? false))) {
foreach (var w in Application.Current.Windows.Cast<Window>().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<Window>().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<Window>().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) {