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);
}