Skip to content

Commit

Permalink
Fixed tasks running on UI thread not detected as still being in tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed May 27, 2024
1 parent dbf82ea commit 949659b
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions FRBDK/Glue/Glue/Tasks/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public class TaskManager : Singleton<TaskManager>

GlueTaskBase CurrentlyRunningTask;

public bool AreAllAsyncTasksDone => TaskCountAccurate == 0;
public bool AreAllAsyncTasksDone =>
TaskCountAccurate == 0

;

/// <summary>
/// Returns the task count, including cancelled tasks.
Expand Down Expand Up @@ -282,6 +285,8 @@ void StartDoTaskManagerLoop()
}
}

const int TaskManagerLoopDelayMs = 50;

async void DoTaskManagerLoop()
{
SyncTaskThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
Expand Down Expand Up @@ -347,7 +352,7 @@ async void DoTaskManagerLoop()
}
else
{
await Task.Delay(50);
await Task.Delay(TaskManagerLoopDelayMs);
}
}
}
Expand Down Expand Up @@ -520,7 +525,9 @@ public async Task<GlueAsyncTask> AddOrRunIfTasked(Func<Task> func, string displa
DoOnUiThread = doOnUiThread
};

TaskAddedOrRemoved?.Invoke(TaskEvent.StartedImmediate, task);
await RunTask(task, markAsCurrent: false);
TaskAddedOrRemoved?.Invoke(TaskEvent.Removed, task);

return task;
}
Expand Down Expand Up @@ -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<string> frameTexts = new List<string>();
List<string> frameTexts = new List<string>();
//for (int i = stackTrace.FrameCount - 1; i > -1; i--)
//{
// var frame = stackTrace.GetFrame(i);
Expand All @@ -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;
}
Expand Down

0 comments on commit 949659b

Please sign in to comment.