diff --git a/addons/GDTask/GDTask.Delay.cs b/addons/GDTask/GDTask.Delay.cs index 047f473..74eb6ad 100644 --- a/addons/GDTask/GDTask.Delay.cs +++ b/addons/GDTask/GDTask.Delay.cs @@ -253,6 +253,7 @@ static NextFramePromise() TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size); } + bool isMainThread; ulong frameCount; CancellationToken cancellationToken; GDTaskCompletionSourceCore core; @@ -273,7 +274,9 @@ public static IGDTaskSource Create(PlayerLoopTiming timing, CancellationToken ca result = new NextFramePromise(); } - result.frameCount = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetProcessFrames() : 0ul; + result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (result.isMainThread) + result.frameCount = Engine.GetProcessFrames(); result.cancellationToken = cancellationToken; TaskTracker.TrackActiveTask(result, 3); @@ -319,7 +322,7 @@ public bool MoveNext() return false; } - if (frameCount == Engine.GetProcessFrames()) + if (isMainThread && frameCount == Engine.GetProcessFrames()) { return true; } @@ -348,6 +351,7 @@ static DelayFramePromise() TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size); } + bool isMainThread; ulong initialFrame; int delayFrameCount; CancellationToken cancellationToken; @@ -373,7 +377,9 @@ public static IGDTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, result.delayFrameCount = delayFrameCount; result.cancellationToken = cancellationToken; - result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetProcessFrames() : 0ul; + result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (result.isMainThread) + result.initialFrame = Engine.GetProcessFrames(); TaskTracker.TrackActiveTask(result, 3); @@ -427,7 +433,7 @@ public bool MoveNext() } // skip in initial frame. - if (initialFrame == Engine.GetProcessFrames()) + if (isMainThread && initialFrame == Engine.GetProcessFrames()) { #if DEBUG // force use Realtime. @@ -476,6 +482,7 @@ static DelayPromise() TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size); } + bool isMainThread; ulong initialFrame; double delayTimeSpan; double elapsed; @@ -502,7 +509,9 @@ public static IGDTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timi result.elapsed = 0.0f; result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds; result.cancellationToken = cancellationToken; - result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetProcessFrames() : 0ul; + result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; + if (result.isMainThread) + result.initialFrame = Engine.GetProcessFrames(); TaskTracker.TrackActiveTask(result, 3); @@ -549,7 +558,7 @@ public bool MoveNext() if (elapsed == 0.0f) { - if (initialFrame == Engine.GetProcessFrames()) + if (isMainThread && initialFrame == Engine.GetProcessFrames()) { return true; } diff --git a/addons/GDTask/PlayerLoopTimer.cs b/addons/GDTask/PlayerLoopTimer.cs index 4709201..aada6bc 100644 --- a/addons/GDTask/PlayerLoopTimer.cs +++ b/addons/GDTask/PlayerLoopTimer.cs @@ -176,7 +176,8 @@ protected override void ResetCore(TimeSpan? interval) { this.elapsed = 0.0; this.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread; - this.initialFrame = Engine.GetProcessFrames(); + if (this.isMainThread) + this.initialFrame = Engine.GetProcessFrames(); if (interval != null) { this.interval = (float)interval.Value.TotalSeconds;