diff --git a/src/Service/NotificationService.cs b/src/Service/NotificationService.cs index 78290cb8..89e84ea6 100644 --- a/src/Service/NotificationService.cs +++ b/src/Service/NotificationService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; @@ -479,8 +479,8 @@ public void Loading(bool running) if (WpfApplication.Current == null || WpfApplication.Current.Dispatcher == null) return; - // Multi-threading trick - WpfApplication.Current.Dispatcher.Invoke((Action)delegate + // Multi-threading safe dispatch + WpfApplication.Current.Dispatcher.BeginInvoke((Action)delegate { Mouse.OverrideCursor = running ? Cursors.Wait : null; @@ -572,7 +572,7 @@ private void StartRotationAnimation() try { - WpfApplication.Current.Dispatcher.Invoke((Action)delegate + WpfApplication.Current.Dispatcher.BeginInvoke((Action)delegate { try { @@ -610,7 +610,7 @@ private void StopRotationAnimation() return; } - WpfApplication.Current.Dispatcher.Invoke((Action)delegate + WpfApplication.Current.Dispatcher.BeginInvoke((Action)delegate { CleanupRotationTimer(); }); diff --git a/src/View/Window/MainWindow.xaml.cs b/src/View/Window/MainWindow.xaml.cs index 24f58b93..486a5a7a 100644 --- a/src/View/Window/MainWindow.xaml.cs +++ b/src/View/Window/MainWindow.xaml.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Threading; using System.Windows; @@ -213,21 +213,25 @@ private void OnOptimizeCommandCompleted() } else { - Thread.Sleep(1000); - App.Shutdown(); + var timer = new System.Windows.Threading.DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; + timer.Tick += (sender, args) => + { + timer.Stop(); + App.Shutdown(); + }; + timer.Start(); } } else { _viewModel.IsBusy = false; - // Wait for the button to be enabled before trying to refocus it - // The button is disabled while IsOptimizationRunning = true + // Wait for the button to be enabled before trying to refocus it Dispatcher.BeginInvoke(new Action(() => - { - SetFocusTo(Optimize, force: true); - }), System.Windows.Threading.DispatcherPriority.Loaded); - } + { + SetFocusTo(Optimize, force: true); + }), System.Windows.Threading.DispatcherPriority.Loaded); + } } /// @@ -348,7 +352,6 @@ private void SetFocusTo(object element, bool force = false) else { // Normal focus - immediate - Keyboard.ClearFocus(); Keyboard.Focus(uiElement); FocusManager.SetFocusedElement(this, inputElement); } diff --git a/src/ViewModel/MainViewModel.cs b/src/ViewModel/MainViewModel.cs index 84feb147..49178e18 100644 --- a/src/ViewModel/MainViewModel.cs +++ b/src/ViewModel/MainViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -1706,7 +1706,12 @@ private void MonitorComputer() { // Check if it's busy if (IsBusy) + { + if (_cancellationTokenSource.Token.WaitHandle.WaitOne(1000)) + break; + continue; + } lock (_lockObject) {