Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DispatcherTaskScheduler instead of TaskScheduler.FromCurrentSynchronizationContext #17825

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Avalonia.Base/Animation/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.Animation.Easings;
using Avalonia.Data;
using Avalonia.Metadata;
using Avalonia.Threading;

namespace Avalonia.Animation
{
Expand Down Expand Up @@ -310,7 +311,7 @@ internal IDisposable Apply(Animatable control, IClock? clock, IObservable<bool>
Task.WhenAll(completionTasks!)
.ContinueWith((_, state) => ((Action)state!).Invoke()
, onComplete
, TaskScheduler.FromCurrentSynchronizationContext()
, DispatcherTaskScheduler.UIThread
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Avalonia.Base/Data/Core/Plugins/TaskStreamPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Threading.Tasks;
using Avalonia.Reactive;
using Avalonia.Threading;

namespace Avalonia.Data.Core.Plugins
{
Expand Down Expand Up @@ -51,7 +52,7 @@ public virtual bool Match(WeakReference<object?> reference)
var subject = new LightweightSubject<object?>();
task.ContinueWith(
x => HandleCompleted(task).Subscribe(subject),
TaskScheduler.FromCurrentSynchronizationContext())
DispatcherTaskScheduler.UIThread)
.ConfigureAwait(false);
return subject;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Avalonia.Base/Data/Core/Plugins/TaskStreamPlugin`1.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Avalonia.Reactive;
using Avalonia.Threading;

namespace Avalonia.Data.Core.Plugins;

Expand All @@ -27,7 +28,7 @@ public bool Match(WeakReference<object?> reference)
var subject = new LightweightSubject<object?>();
task.ContinueWith(
_ => HandleCompleted(task).Subscribe(subject),
TaskScheduler.FromCurrentSynchronizationContext())
DispatcherTaskScheduler.UIThread)
.ConfigureAwait(false);
return subject;
}
Expand Down
39 changes: 39 additions & 0 deletions src/Avalonia.Base/Threading/DispatcherTaskScheduler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Avalonia.Threading;

internal class DispatcherTaskScheduler : TaskScheduler
{
private static DispatcherTaskScheduler? s_uiThread;
public static DispatcherTaskScheduler UIThread => s_uiThread ??= new DispatcherTaskScheduler(Dispatcher.UIThread);

private readonly Dispatcher _dispatcher;
private readonly SendOrPostCallback _postCallback;

public DispatcherTaskScheduler(Dispatcher dispatcher)
{
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
_postCallback = QueueTaskCallback;
}

protected override IEnumerable<Task>? GetScheduledTasks() => null;

protected override void QueueTask(Task task)
{
_dispatcher.Post(_postCallback, task);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i. e. this code would run continuations with a rather low priority which is not suitable for animations.

}

protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
if (!_dispatcher.CheckAccess())
return false;

return TryExecuteTask(task);
}

private void QueueTaskCallback(object? state) => TryExecuteTask((Task)state!);
}
3 changes: 2 additions & 1 deletion src/Avalonia.Controls/TransitioningContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.Threading;

namespace Avalonia.Controls;

Expand Down Expand Up @@ -103,7 +104,7 @@ Presenter is { } presenter &&
{
HideOldPresenter();
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}, DispatcherTaskScheduler.UIThread);
}

_shouldAnimate = false;
Expand Down
3 changes: 2 additions & 1 deletion src/Avalonia.Controls/VirtualizingCarouselPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.Animation;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Threading;

namespace Avalonia.Controls
{
Expand Down Expand Up @@ -167,7 +168,7 @@ _realized is { } to &&
}

transition.Start(_transitionFrom, to, forward, _transition.Token)
.ContinueWith(TransitionFinished, TaskScheduler.FromCurrentSynchronizationContext());
.ContinueWith(TransitionFinished, DispatcherTaskScheduler.UIThread);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Data;
using Avalonia.Data.Core.Plugins;
using Avalonia.Reactive;
using Avalonia.Threading;

namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings
{
Expand All @@ -29,7 +30,7 @@ public bool Match(WeakReference<object?> reference)
var subject = new LightweightSubject<object?>();
task.ContinueWith(
_ => HandleCompleted(task).Subscribe(subject),
TaskScheduler.FromCurrentSynchronizationContext())
DispatcherTaskScheduler.UIThread)
.ConfigureAwait(false);
return subject;
}
Expand Down
Loading