From ec861c485cea94fe755182ce9a6d9d34a37b0a77 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Fri, 20 Dec 2024 15:36:23 +0100 Subject: [PATCH] fix for unit tests in CloseApplicationWatcherBase --- .../ApplicationWatchers/ApplicationWatcherBase.cs | 6 +++--- .../CloseApplicationWatcherBase.cs | 13 +++++++++++-- .../Services/CloseApplicationWatcherBaseFacts.cs | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Orchestra.Core/ApplicationWatchers/ApplicationWatcherBase.cs b/src/Orchestra.Core/ApplicationWatchers/ApplicationWatcherBase.cs index 7ee9cb729..dd0878a18 100644 --- a/src/Orchestra.Core/ApplicationWatchers/ApplicationWatcherBase.cs +++ b/src/Orchestra.Core/ApplicationWatchers/ApplicationWatcherBase.cs @@ -5,10 +5,10 @@ using System.Linq; using System.Threading.Tasks; using System.Windows; - using System.Windows.Threading; using Catel.IoC; using Catel.Logging; using Catel.Services; + using Catel.Windows.Threading; using Orchestra.Services; public abstract class ApplicationWatcherBase @@ -18,7 +18,7 @@ public abstract class ApplicationWatcherBase protected static readonly IDispatcherService DispatcherService; protected static readonly IMainWindowService MainWindowService; - private static readonly DispatcherTimer DispatcherTimer; + private static readonly DispatcherTimerEx DispatcherTimer; private static readonly Queue> ShellActivatedActions; private static readonly object Lock = new object(); @@ -30,7 +30,7 @@ static ApplicationWatcherBase() DispatcherService = serviceLocator.ResolveRequiredType(); MainWindowService = serviceLocator.ResolveRequiredType(); - DispatcherTimer = new DispatcherTimer(); + DispatcherTimer = new DispatcherTimerEx(DispatcherService); DispatcherTimer.Interval = TimeSpan.FromMilliseconds(5); DispatcherTimer.Tick += async (sender, e) => await EnsureMainWindowAsync(); DispatcherTimer.Start(); diff --git a/src/Orchestra.Core/ApplicationWatchers/CloseApplicationWatcherBase.cs b/src/Orchestra.Core/ApplicationWatchers/CloseApplicationWatcherBase.cs index 424505a24..fdd07aecb 100644 --- a/src/Orchestra.Core/ApplicationWatchers/CloseApplicationWatcherBase.cs +++ b/src/Orchestra.Core/ApplicationWatchers/CloseApplicationWatcherBase.cs @@ -245,13 +245,22 @@ private static async Task HandleClosingErrorAsync(Window window, Exception ex) if (await MessageService.ShowAsync(closingDetails.Message, "Error", messageButton, MessageImage.Error) == MessageResult.OK) { - await CloseWindowAsync(window).ConfigureAwait(false); + await CloseWindowAsync(window); } } private static async Task CloseWindowAsync(Window window) { - await DispatcherService.InvokeAsync(window.Close).ConfigureAwait(false); + try + { + // Note: always use window dispatcher + await window.Dispatcher.InvokeAsync(window.Close); + //await DispatcherService.InvokeAsync(window.Close).ConfigureAwait(false); + } + catch (Exception) + { + // ignore + } } private static void NotifyClosingCanceled() diff --git a/src/Orchestra.Tests/Services/CloseApplicationWatcherBaseFacts.cs b/src/Orchestra.Tests/Services/CloseApplicationWatcherBaseFacts.cs index 17349ebca..7b5d8cddf 100644 --- a/src/Orchestra.Tests/Services/CloseApplicationWatcherBaseFacts.cs +++ b/src/Orchestra.Tests/Services/CloseApplicationWatcherBaseFacts.cs @@ -38,6 +38,7 @@ private async Task RunOnWindowClosingAndWaitForFinishAsync(TestCloseApplicationW ArgumentNullException.ThrowIfNull(watcher); bool isWatcherCompleted = false; + using (var cts = new CancellationTokenSource(timeout)) { // Use a semaphore to prevent the [TestMethod] from returning prematurely.