Browse Source

API change requests

pull/21032/head
Tom Edwards 3 weeks ago
parent
commit
92fc7aca7d
  1. 25
      src/Avalonia.Base/Threading/Dispatcher.TaskScheduler.cs
  2. 2
      tests/Avalonia.Base.UnitTests/DispatcherTests.cs

25
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)
/// <summary>
/// Gets a <see cref="TaskScheduler"/> which executes tasks on this <see cref="Dispatcher"/>. A <see cref="DispatcherPriority"/> is captured from
/// the current <see cref="AvaloniaSynchronizationContext"/> if one is available. Otherwise, <see cref="DispatcherPriority.Default"/> is used.
/// </summary>
public TaskScheduler ToTaskScheduler() => ToTaskScheduler(SynchronizationContext.Current switch
{
lock (dispatcher.InstanceLock)
AvaloniaSynchronizationContext avaloniaContext => avaloniaContext.Priority,
_ => DispatcherPriority.Default
});
/// <summary>
/// Gets a <see cref="TaskScheduler"/> which executes tasks on this <see cref="Dispatcher"/> with the specified <see cref="DispatcherPriority"/>.
/// </summary>
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;
}
}

2
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);

Loading…
Cancel
Save