Browse Source

Fixed some timers

Nikita Tsukanov 2 years ago
parent
commit
718089bda9

+ 6 - 2
src/Avalonia.Base/Threading/IDispatcherImpl.cs

@@ -30,7 +30,7 @@ public interface IControlledDispatcherImpl : IDispatcherImplWithPendingInput
     void RunLoop(CancellationToken token);
 }
 
-internal class LegacyDispatcherImpl : IControlledDispatcherImpl
+internal class LegacyDispatcherImpl : DefaultDispatcherClock, IControlledDispatcherImpl
 {
     private readonly IPlatformThreadingInterface _platformThreading;
     private IDisposable? _timer;
@@ -50,10 +50,14 @@ internal class LegacyDispatcherImpl : IControlledDispatcherImpl
     {
         _timer?.Dispose();
         _timer = null;
+
         if (dueTimeInMs.HasValue)
+        {
+            var interval = Math.Max(1, dueTimeInMs.Value - TickCount);
             _timer = _platformThreading.StartTimer(DispatcherPriority.Send,
-                TimeSpan.FromMilliseconds(dueTimeInMs.Value),
+                TimeSpan.FromMilliseconds(interval),
                 OnTick);
+        }
     }
 
     private void OnTick()

+ 1 - 1
src/Avalonia.Controls/Platform/ManagedDispatcherImpl.cs

@@ -46,7 +46,7 @@ public class ManagedDispatcherImpl : IControlledDispatcherImpl
         {
             _nextTimer = dueTimeInMs == null
                 ? null
-                : _clock.Elapsed + TimeSpan.FromMilliseconds(dueTimeInMs.Value);
+                : TimeSpan.FromMilliseconds(dueTimeInMs.Value);
             if (!CurrentThreadIsLoopThread)
                 _wakeup.Set();
         }