From ce942b0585fa7fbc253b4a929cd2ece387b7d7e6 Mon Sep 17 00:00:00 2001 From: Seraphima Zykova Date: Mon, 25 Oct 2021 13:05:44 +0300 Subject: [PATCH] [FancyZones Editor] Crash fix (#13930) --- .../editor/FancyZonesEditor/App.xaml.cs | 29 ++++++++------ .../editor/FancyZonesEditor/Logs/Logger.cs | 2 +- .../FancyZonesEditor/MainWindow.xaml.cs | 38 ++++++++++--------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs index ac51831ac1..29eabeaea4 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs @@ -37,6 +37,8 @@ namespace FancyZonesEditor private EventWaitHandle _eventHandle; + private Thread _exitWaitThread; + public static bool DebugMode { get @@ -61,17 +63,8 @@ namespace FancyZonesEditor Overlay = new Overlay(); MainWindowSettings = new MainWindowSettingsModel(); - new Thread(() => - { - _eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, interop.Constants.FZEExitEvent()); - if (_eventHandle.WaitOne()) - { - Logger.LogInfo("FancyZones disabled, exit"); - Environment.Exit(0); - } - }).Start(); - - Logger.LogInfo("FancyZones Editor started"); + _exitWaitThread = new Thread(App_WaitExit); + _exitWaitThread.Start(); } private void OnStartup(object sender, StartupEventArgs e) @@ -132,14 +125,28 @@ namespace FancyZonesEditor private void OnExit(object sender, ExitEventArgs e) { + Dispose(); + if (_eventHandle != null) { _eventHandle.Set(); } + _exitWaitThread.Join(); + Logger.LogInfo("FancyZones Editor exited"); } + private void App_WaitExit() + { + _eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, interop.Constants.FZEExitEvent()); + if (_eventHandle.WaitOne()) + { + Logger.LogInfo("Exit event triggered"); + Environment.Exit(0); + } + } + public void App_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == System.Windows.Input.Key.LeftShift || e.Key == System.Windows.Input.Key.RightShift) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Logs/Logger.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Logs/Logger.cs index 63c6080244..1998ddd516 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Logs/Logger.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Logs/Logger.cs @@ -95,7 +95,7 @@ namespace FancyZonesEditor.Logs var methodName = stackTrace.GetFrame(3)?.GetMethod(); var className = methodName?.DeclaringType.Name; - return className + " :: " + methodName?.Name; + return className + "::" + methodName?.Name; } } } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs index d921fff378..36718455da 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs @@ -4,8 +4,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; using System.Windows; using System.Windows.Automation; using System.Windows.Automation.Peers; @@ -43,20 +41,7 @@ namespace FancyZonesEditor DataContext = _settings; KeyUp += MainWindow_KeyUp; - - // Prevent closing the dialog with enter - PreviewKeyDown += (object sender, KeyEventArgs e) => - { - if (e.Key == Key.Enter && _openedDialog != null && _openedDialog.IsVisible) - { - var source = e.OriginalSource as RadioButton; - if (source != null && source.IsChecked != true) - { - source.IsChecked = true; - e.Handled = true; - } - } - }; + PreviewKeyDown += MainWindow_PreviewKeyDown; if (spanZonesAcrossMonitors) { @@ -86,6 +71,20 @@ namespace FancyZonesEditor } } + // Prevent closing the dialog with enter + private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Enter && _openedDialog != null && _openedDialog.IsVisible) + { + var source = e.OriginalSource as RadioButton; + if (source != null && source.IsChecked != true) + { + source.IsChecked = true; + e.Handled = true; + } + } + } + private void LayoutItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) { CloseDialog(sender); @@ -256,7 +255,7 @@ namespace FancyZonesEditor private void Announce(string name, string message) { - if (AutomationPeer.ListenerExists(AutomationEvents.MenuOpened)) + if (AutomationPeer.ListenerExists(AutomationEvents.MenuOpened) && _createLayoutAnnounce != null) { var peer = UIElementAutomationPeer.FromElement(_createLayoutAnnounce); AutomationProperties.SetName(_createLayoutAnnounce, name + " " + message); @@ -516,7 +515,10 @@ namespace FancyZonesEditor private void TextBox_GotKeyboardFocus(object sender, RoutedEventArgs e) { TextBox tb = sender as TextBox; - tb.SelectionStart = tb.Text.Length; + if (tb != null) + { + tb.SelectionStart = tb.Text.Length; + } } private void CancelLayoutChanges()