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

Make dispatcher more usable on non-ui threads #12240

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
7 changes: 4 additions & 3 deletions src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public void Dispose()
}
}

public static RestoreContext Ensure(DispatcherPriority priority)
public static RestoreContext Ensure(DispatcherPriority priority) => Ensure(Dispatcher.UIThread, priority);
public static RestoreContext Ensure(Dispatcher dispatcher, DispatcherPriority priority)
{
if (Current is AvaloniaSynchronizationContext avaloniaContext
&& avaloniaContext.Priority == priority)
return default;
var oldContext = Current;
Dispatcher.UIThread.VerifyAccess();
SetSynchronizationContext(Dispatcher.UIThread.GetContextWithPriority(priority));
dispatcher.VerifyAccess();
SetSynchronizationContext(dispatcher.GetContextWithPriority(priority));
return new RestoreContext(oldContext);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.Base/Threading/Dispatcher.Invoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void Invoke(Action callback, DispatcherPriority priority, CancellationTok
// call the callback directly.
if (!cancellationToken.IsCancellationRequested && priority == DispatcherPriority.Send && CheckAccess())
{
using (AvaloniaSynchronizationContext.Ensure(priority))
using (AvaloniaSynchronizationContext.Ensure(this, priority))
callback();
return;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public TResult Invoke<TResult>(Func<TResult> callback, DispatcherPriority priori
// call the callback directly.
if (!cancellationToken.IsCancellationRequested && priority == DispatcherPriority.Send && CheckAccess())
{
using (AvaloniaSynchronizationContext.Ensure(priority))
using (AvaloniaSynchronizationContext.Ensure(this, priority))
return callback();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Threading/Dispatcher.MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void PushFrame(DispatcherFrame frame)
try
{
_frames.Push(frame);
using (AvaloniaSynchronizationContext.Ensure(DispatcherPriority.Normal))
using (AvaloniaSynchronizationContext.Ensure(this, DispatcherPriority.Normal))
frame.Run(_controlledImpl);
}
finally
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Threading/DispatcherOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ internal void Execute()

try
{
using (AvaloniaSynchronizationContext.Ensure(Priority))
using (AvaloniaSynchronizationContext.Ensure(Dispatcher, Priority))
InvokeCore();
}
finally
Expand Down