From 23baa0a0433ccda53b59d39fa0e3ab4848a85d91 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Mon, 30 Mar 2026 13:54:32 +0500 Subject: [PATCH] Added explicit background processing support to managed dispatcher impl (#21025) --- .../Platform/ManagedDispatcherImpl.cs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Platform/ManagedDispatcherImpl.cs b/src/Avalonia.Base/Platform/ManagedDispatcherImpl.cs index c8225f775b..fc1cfbf5b1 100644 --- a/src/Avalonia.Base/Platform/ManagedDispatcherImpl.cs +++ b/src/Avalonia.Base/Platform/ManagedDispatcherImpl.cs @@ -7,7 +7,7 @@ using Avalonia.Threading; namespace Avalonia.Controls.Platform; [Unstable] -public class ManagedDispatcherImpl : IControlledDispatcherImpl +public class ManagedDispatcherImpl : IControlledDispatcherImpl, IDispatcherImplWithExplicitBackgroundProcessing { private readonly IManagedDispatcherInputProvider? _inputProvider; private readonly AutoResetEvent _wakeup = new(false); @@ -16,6 +16,7 @@ public class ManagedDispatcherImpl : IControlledDispatcherImpl private readonly Stopwatch _clock = Stopwatch.StartNew(); private TimeSpan? _nextTimer; private readonly Thread _loopThread = Thread.CurrentThread; + private bool _backgroundProcessingRequested; public interface IManagedDispatcherInputProvider { @@ -56,6 +57,22 @@ public class ManagedDispatcherImpl : IControlledDispatcherImpl public bool CanQueryPendingInput => _inputProvider != null; public bool HasPendingInput => _inputProvider?.HasInput ?? false; + private Action? ReadyForBackgroundProcessing { get; set; } + event Action? IDispatcherImplWithExplicitBackgroundProcessing.ReadyForBackgroundProcessing + { + add => ReadyForBackgroundProcessing += value; + remove => ReadyForBackgroundProcessing -= value; + } + + void IDispatcherImplWithExplicitBackgroundProcessing.RequestBackgroundProcessing() + { + lock (_lock) + { + _backgroundProcessingRequested = true; + _wakeup.Set(); + } + } + public void RunLoop(CancellationToken token) { CancellationTokenRegistration registration = default; @@ -98,6 +115,19 @@ public class ManagedDispatcherImpl : IControlledDispatcherImpl _inputProvider.DispatchNextInputEvent(); continue; } + + bool triggerBackgroundProcessing; + lock (_lock) + { + triggerBackgroundProcessing = _backgroundProcessingRequested; + _backgroundProcessingRequested = false; + } + + if (triggerBackgroundProcessing) + { + ReadyForBackgroundProcessing?.Invoke(); + continue; + } TimeSpan? nextTimer; lock (_lock)