From 949659b06d93655b8494ad11b1effc56bdc157af Mon Sep 17 00:00:00 2001 From: Victor Chelaru Date: Mon, 27 May 2024 13:56:39 -0600 Subject: [PATCH] Fixed tasks running on UI thread not detected as still being in tasks. --- FRBDK/Glue/Glue/Tasks/TaskManager.cs | 46 +++++++++++++++------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/FRBDK/Glue/Glue/Tasks/TaskManager.cs b/FRBDK/Glue/Glue/Tasks/TaskManager.cs index 81d460989..3f5311998 100644 --- a/FRBDK/Glue/Glue/Tasks/TaskManager.cs +++ b/FRBDK/Glue/Glue/Tasks/TaskManager.cs @@ -68,7 +68,10 @@ public class TaskManager : Singleton GlueTaskBase CurrentlyRunningTask; - public bool AreAllAsyncTasksDone => TaskCountAccurate == 0; + public bool AreAllAsyncTasksDone => + TaskCountAccurate == 0 + + ; /// /// Returns the task count, including cancelled tasks. @@ -282,6 +285,8 @@ void StartDoTaskManagerLoop() } } + const int TaskManagerLoopDelayMs = 50; + async void DoTaskManagerLoop() { SyncTaskThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId; @@ -347,7 +352,7 @@ async void DoTaskManagerLoop() } else { - await Task.Delay(50); + await Task.Delay(TaskManagerLoopDelayMs); } } } @@ -520,7 +525,9 @@ public async Task AddOrRunIfTasked(Func func, string displa DoOnUiThread = doOnUiThread }; + TaskAddedOrRemoved?.Invoke(TaskEvent.StartedImmediate, task); await RunTask(task, markAsCurrent: false); + TaskAddedOrRemoved?.Invoke(TaskEvent.Removed, task); return task; } @@ -794,10 +801,13 @@ public bool IsInTask() return true; } - //var stackTrace = new System.Diagnostics.StackTrace(); + // It's possible we may not be in a task based on threads, but we are + // still in the task based on callstack because we're running on the UI thread. + // So we need to do the expensive operation of checking the callstack. + var stackTrace = new System.Diagnostics.StackTrace(); //var stackFrame = new System.Diagnostics.StackFrame(); - //List frameTexts = new List(); + List frameTexts = new List(); //for (int i = stackTrace.FrameCount - 1; i > -1; i--) //{ // var frame = stackTrace.GetFrame(i); @@ -823,24 +833,18 @@ public bool IsInTask() } } */ - //for (int i = stackTrace.FrameCount - 1; i > -1; i--) - //{ - // var frame = stackTrace.GetFrame(i); - // var frameText = frame.ToString(); + for (int i = stackTrace.FrameCount - 1; i > -1; i--) + { + var frame = stackTrace.GetFrame(i); + var frameText = frame.ToString(); - // var isTasked = frameText.StartsWith("RunOnUiThreadTasked") || - // // Vic says - not sure why but sometimes thread IDs change when in an async function. - // // So I thought I could check if the thread is the main task thread, but this won't work - // // because command receiving from the game runs on a separate thread, so that would behave - // // as if it's tasked, even though it's not - // // so we check this: - // frameText.StartsWith(nameof(GlueTask.Do_Action_Internal) + " "); - - // if (isTasked) - // { - // return true; - // } - //} + var isTasked = frameText.StartsWith("RunOnUiThreadTasked"); + + if (isTasked) + { + return true; + } + } return false; }