diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp/Views/MainWindow.axaml b/src/GuiApp/Avalonia/FileTime.GuiApp/Views/MainWindow.axaml index e5654e9..0d88e11 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp/Views/MainWindow.axaml +++ b/src/GuiApp/Avalonia/FileTime.GuiApp/Views/MainWindow.axaml @@ -1,6 +1,7 @@ ? _openModals; private ReadInputsViewModel? _inputViewModel; private IDisposable? _inputViewModelSubscription; + private bool _isShuttingDown; + private bool _shutdownCompleted; + private readonly object _isClosingLock = new(); public MainWindowViewModel? ViewModel { @@ -167,15 +170,9 @@ public partial class MainWindow : Window, IUiAccessor } } - private void OnWindowClosed(object? sender, EventArgs e) + + private void Window_OnClosed(object? sender, EventArgs e) { - var vm = ViewModel; - Task.Run(async () => - { - if (vm is null) return; - await vm.OnExit(); - }) - .Wait(); } private void InputList_OnKeyUp(object? sender, KeyEventArgs e) @@ -219,4 +216,52 @@ public partial class MainWindow : Window, IUiAccessor } } } + + private void Window_OnClosing(object? sender, WindowClosingEventArgs e) + { + lock (_isClosingLock) + { + if (_isShuttingDown) + { + e.Cancel = true; + return; + } + + if (_shutdownCompleted) + { + return; + } + + _isShuttingDown = true; + e.Cancel = true; + + var vm = ViewModel; + var exitVm = new MainWindowLoadingViewModel(); + exitVm.Title.SetValueSafe("Shutting down..."); + DataContext = exitVm; + + Task.Run(async () => + { + await Task.Delay(200); + try + { + if (vm is not null) + { + await vm.OnExit(); + } + } + catch + { + } + + lock (_isClosingLock) + { + _isShuttingDown = false; + _shutdownCompleted = true; + } + + Dispatcher.UIThread.Invoke(Close); + }); + } + } } \ No newline at end of file