From 92fc7aca7d3a79eb41f16840d37b38b641d22d04 Mon Sep 17 00:00:00 2001 From: Tom Edwards Date: Sat, 9 May 2026 15:16:33 +0200 Subject: [PATCH] API change requests --- .../Threading/Dispatcher.TaskScheduler.cs | 25 ++++++++++++++----- .../DispatcherTests.cs | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Base/Threading/Dispatcher.TaskScheduler.cs b/src/Avalonia.Base/Threading/Dispatcher.TaskScheduler.cs index d3bb0767c7..b39f619430 100644 --- a/src/Avalonia.Base/Threading/Dispatcher.TaskScheduler.cs +++ b/src/Avalonia.Base/Threading/Dispatcher.TaskScheduler.cs @@ -7,17 +7,30 @@ public partial class Dispatcher { private TaskScheduler? _taskScheduler; - public static implicit operator TaskScheduler(Dispatcher dispatcher) + /// + /// Gets a which executes tasks on this . A is captured from + /// the current if one is available. Otherwise, is used. + /// + public TaskScheduler ToTaskScheduler() => ToTaskScheduler(SynchronizationContext.Current switch { - lock (dispatcher.InstanceLock) + AvaloniaSynchronizationContext avaloniaContext => avaloniaContext.Priority, + _ => DispatcherPriority.Default + }); + + /// + /// Gets a which executes tasks on this with the specified . + /// + public TaskScheduler ToTaskScheduler(DispatcherPriority priority) + { + lock (InstanceLock) { - if (dispatcher._taskScheduler == null) + if (_taskScheduler == null) { var prevContext = SynchronizationContext.Current; - SynchronizationContext.SetSynchronizationContext(dispatcher.GetContextWithPriority(DispatcherPriority.Default)); + SynchronizationContext.SetSynchronizationContext(GetContextWithPriority(priority)); try { - dispatcher._taskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); + _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); } finally { @@ -26,6 +39,6 @@ public partial class Dispatcher } } - return dispatcher._taskScheduler; + return _taskScheduler; } } diff --git a/tests/Avalonia.Base.UnitTests/DispatcherTests.cs b/tests/Avalonia.Base.UnitTests/DispatcherTests.cs index 2dde016c24..2ab442e4fa 100644 --- a/tests/Avalonia.Base.UnitTests/DispatcherTests.cs +++ b/tests/Avalonia.Base.UnitTests/DispatcherTests.cs @@ -854,7 +854,7 @@ public partial class DispatcherTests var impl = new SimpleDispatcherImpl(); Dispatcher.InitializeUIThreadDispatcher(impl); Thread? continuationThread = null; - _ = Task.CompletedTask.ContinueWith(t => continuationThread = Thread.CurrentThread, Dispatcher.UIThread); + _ = Task.CompletedTask.ContinueWith(t => continuationThread = Thread.CurrentThread, Dispatcher.UIThread.ToTaskScheduler()); Assert.True(impl.AskedForSignal); impl.ExecuteSignal(); Assert.Equal(Dispatcher.UIThread.Thread, continuationThread);