From 3b259812c463423d9af4d914e0f630aeefd5e4cc Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Fri, 28 Aug 2026 12:28:19 +0000 Subject: [PATCH] Fixed dispatcher frame capturing wrong dispatcher (#22086) --- src/Avalonia.Base/Threading/DispatcherFrame.cs | 4 ++-- tests/Avalonia.Base.UnitTests/DispatcherTests.cs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Base/Threading/DispatcherFrame.cs b/src/Avalonia.Base/Threading/DispatcherFrame.cs index f4a91ab800..e3e85c71e0 100644 --- a/src/Avalonia.Base/Threading/DispatcherFrame.cs +++ b/src/Avalonia.Base/Threading/DispatcherFrame.cs @@ -38,7 +38,7 @@ public class DispatcherFrame /// for their important criteria to be met. These frames /// should have a timeout associated with them. /// - public DispatcherFrame(bool exitWhenRequested) : this(Dispatcher.UIThread, exitWhenRequested) + public DispatcherFrame(bool exitWhenRequested) : this(Dispatcher.CurrentDispatcher, exitWhenRequested) { Dispatcher.VerifyAccess(); } @@ -139,4 +139,4 @@ public class DispatcherFrame if (_exitWhenRequested) _cancellationTokenSource?.Cancel(); } -} \ No newline at end of file +} diff --git a/tests/Avalonia.Base.UnitTests/DispatcherTests.cs b/tests/Avalonia.Base.UnitTests/DispatcherTests.cs index d4e39d34b6..8f8d8d87a9 100644 --- a/tests/Avalonia.Base.UnitTests/DispatcherTests.cs +++ b/tests/Avalonia.Base.UnitTests/DispatcherTests.cs @@ -317,6 +317,21 @@ public partial class DispatcherTests } } + [Fact] + public async Task DispatcherFrame_Uses_Current_Dispatcher() + { + var uiThreadDispatcher = Dispatcher.UIThread; + + await ThreadRunHelper.RunOnDedicatedThread(() => + { + var currentDispatcher = Dispatcher.CurrentDispatcher; + var frame = new DispatcherFrame(); + + Assert.NotSame(uiThreadDispatcher, currentDispatcher); + Assert.Same(currentDispatcher, frame.Dispatcher); + }); + } + [Fact] public void ExitAllFramesShouldExitAllFramesAndBeAbleToContinue() {