App: Add ShutdownNow() and ShutdownGracefully()
This commit is contained in:
+18
-13
@@ -18,12 +18,13 @@ using System.Reflection;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using System.Windows.Input;
|
||||||
using System.Windows.Threading;
|
using System.Windows.Threading;
|
||||||
|
|
||||||
namespace Elwig {
|
namespace Elwig {
|
||||||
public partial class App : Application {
|
public partial class App : Application {
|
||||||
|
|
||||||
protected static App CurrentApp;
|
public static App CurrentApp;
|
||||||
public static int NumWindows => CurrentApp.Windows.Count;
|
public static int NumWindows => CurrentApp.Windows.Count;
|
||||||
public static bool ForceShutdown { get; set; } = false;
|
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);
|
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))
|
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();
|
await BackupService.RestoreDatabase();
|
||||||
Shutdown();
|
ShutdownNow();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +120,7 @@ namespace Elwig {
|
|||||||
InteractionService.ShowException("Fehler", "Fehler beim Laden der Mandantendaten, unbedingt den Programmierern melden!", exc);
|
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))
|
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();
|
await BackupService.RestoreDatabase();
|
||||||
Shutdown();
|
ShutdownNow();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BranchNum = branches.Count;
|
BranchNum = branches.Count;
|
||||||
@@ -183,7 +184,7 @@ namespace Elwig {
|
|||||||
if (Config.Branch != null) {
|
if (Config.Branch != null) {
|
||||||
if (!branches.ContainsKey(Config.Branch.ToLower())) {
|
if (!branches.ContainsKey(Config.Branch.ToLower())) {
|
||||||
InteractionService.ShowError("Ungültige Zweigstelle", "Ungültige Zweigstelle in Konfigurationsdatei!");
|
InteractionService.ShowError("Ungültige Zweigstelle", "Ungültige Zweigstelle in Konfigurationsdatei!");
|
||||||
Shutdown();
|
ShutdownNow();
|
||||||
} else {
|
} else {
|
||||||
SetBranch(branches[Config.Branch.ToLower()]);
|
SetBranch(branches[Config.Branch.ToLower()]);
|
||||||
}
|
}
|
||||||
@@ -191,7 +192,7 @@ namespace Elwig {
|
|||||||
SetBranch(branches.First().Value);
|
SetBranch(branches.First().Value);
|
||||||
} else {
|
} else {
|
||||||
InteractionService.ShowError("Ungültige Zweigstelle", "Erkennen der lokalen Zweigstelle nicht möglich!");
|
InteractionService.ShowError("Ungültige Zweigstelle", "Erkennen der lokalen Zweigstelle nicht möglich!");
|
||||||
Shutdown();
|
ShutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Config.WeighingMode == null) {
|
if (Config.WeighingMode == null) {
|
||||||
@@ -210,21 +211,27 @@ namespace Elwig {
|
|||||||
window.Show();
|
window.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnClosing() {
|
public static void ShutdownNow() {
|
||||||
|
ForceShutdown = true;
|
||||||
|
Current.Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void ShutdownGracefully() {
|
||||||
|
Mouse.OverrideCursor = Cursors.AppStarting;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await BackupService.TryDatabaseBackup();
|
await BackupService.TryDatabaseBackup();
|
||||||
} catch (Exception exc) {
|
} catch (Exception exc) {
|
||||||
InteractionService.ShowException("Automatisches Backup", exc);
|
InteractionService.ShowException("Automatisches Backup", exc);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnExit(ExitEventArgs evt) {
|
|
||||||
SerialPortWatcher.Dispose();
|
SerialPortWatcher.Dispose();
|
||||||
foreach (var s in EventScales) {
|
foreach (var s in EventScales) {
|
||||||
s.Dispose();
|
s.Dispose();
|
||||||
}
|
}
|
||||||
Pdf.Cleanup();
|
Pdf.Cleanup();
|
||||||
base.OnExit(evt);
|
|
||||||
|
Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetBranch(Branch b) {
|
public static void SetBranch(Branch b) {
|
||||||
@@ -335,8 +342,7 @@ namespace Elwig {
|
|||||||
await MainDispatcher.BeginInvoke(() => {
|
await MainDispatcher.BeginInvoke(() => {
|
||||||
var d = new UpdateDialog(latest.Value.Version, latest.Value.Url, latest.Value.Size);
|
var d = new UpdateDialog(latest.Value.Version, latest.Value.Url, latest.Value.Size);
|
||||||
if (d.ShowDialog() == true) {
|
if (d.ShowDialog() == true) {
|
||||||
ForceShutdown = true;
|
ShutdownNow();
|
||||||
Current.Shutdown();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (showAlert) {
|
} else if (showAlert) {
|
||||||
@@ -354,8 +360,7 @@ namespace Elwig {
|
|||||||
await Database.Import(filename);
|
await Database.Import(filename);
|
||||||
});
|
});
|
||||||
InteractionService.ShowInformation("Datenbank ersetzen", "Das Ersetzen war erfolgreich!\n\nBitte starten Sie Elwig neu!");
|
InteractionService.ShowInformation("Datenbank ersetzen", "Das Ersetzen war erfolgreich!\n\nBitte starten Sie Elwig neu!");
|
||||||
ForceShutdown = true;
|
ShutdownNow();
|
||||||
Current.Shutdown();
|
|
||||||
} catch (Exception exc) {
|
} catch (Exception exc) {
|
||||||
InteractionService.ShowException("Datenbank ersetzen", "Fehler beim Ersetzen", exc);
|
InteractionService.ShowException("Datenbank ersetzen", "Fehler beim Ersetzen", exc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,12 +108,20 @@ namespace Elwig.Windows {
|
|||||||
LockInputs();
|
LockInputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void TryClose() {
|
||||||
|
var evt = new CancelEventArgs();
|
||||||
|
OnClosing(this, evt);
|
||||||
|
if (!evt.Cancel) Close();
|
||||||
|
}
|
||||||
|
|
||||||
private void OnClosing(object? sender, CancelEventArgs evt) {
|
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?")) {
|
if (!InteractionService.AskConfirmation("Schließen bestätigen", "Soll das Fenster wirklich geschlossen werden?")) {
|
||||||
evt.Cancel = true;
|
evt.Cancel = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} catch { }
|
||||||
}
|
}
|
||||||
IsClosing = true;
|
IsClosing = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,27 +50,23 @@ namespace Elwig.Windows {
|
|||||||
_syncTimer.Start();
|
_syncTimer.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void Window_Closing(object sender, CancelEventArgs evt) {
|
private void Window_Closing(object sender, CancelEventArgs evt) {
|
||||||
evt.Cancel = !App.ForceShutdown;
|
|
||||||
|
|
||||||
if (App.NumWindows > 1 && !App.ForceShutdown) {
|
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 {
|
try {
|
||||||
w.Close();
|
w.Close();
|
||||||
} catch { }
|
} catch { }
|
||||||
}
|
}
|
||||||
Thread.Sleep(100);
|
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 (App.NumWindows > 1) {
|
||||||
if (!InteractionService.AskConfirmation("Elwig beenden", "Es sind noch weitere Fenster geöffnet.\nSollen alle Fenster geschlossen werden?")) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
App.ForceShutdown = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
App.CurrentApp.ShutdownGracefully();
|
||||||
Mouse.OverrideCursor = Cursors.AppStarting;
|
|
||||||
await (Application.Current as App)!.OnClosing();
|
|
||||||
Application.Current.Shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Menu_Help_About_Click(object sender, RoutedEventArgs evt) {
|
private void Menu_Help_About_Click(object sender, RoutedEventArgs evt) {
|
||||||
|
|||||||
Reference in New Issue
Block a user