diff --git a/Dalamud/Logging/Internal/TaskTracker.cs b/Dalamud/Logging/Internal/TaskTracker.cs index 6f51f16a6..2ed3ccbbe 100644 --- a/Dalamud/Logging/Internal/TaskTracker.cs +++ b/Dalamud/Logging/Internal/TaskTracker.cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Threading.Tasks; using Dalamud.Game; -using MonoMod.RuntimeDetour; using Serilog; namespace Dalamud.Logging.Internal @@ -16,8 +16,10 @@ namespace Dalamud.Logging.Internal internal class TaskTracker : IDisposable { private static readonly List TrackedTasksInternal = new(); + private static readonly ConcurrentQueue NewlyCreatedTasks = new(); + private static bool clearRequested = false; - private Hook? scheduleAndStartHook; + private MonoMod.RuntimeDetour.Hook? scheduleAndStartHook; /// /// Initializes a new instance of the class. @@ -38,15 +40,27 @@ namespace Dalamud.Logging.Internal /// /// Clear the list of tracked tasks. /// - public static void Clear() => TrackedTasksInternal.Clear(); + public static void Clear() => clearRequested = true; /// /// Update the tracked data. /// public static void UpdateData() { - foreach (var taskInfo in TrackedTasksInternal) + if (clearRequested) { + TrackedTasksInternal.Clear(); + clearRequested = false; + } + + while (NewlyCreatedTasks.TryDequeue(out var newTask)) + { + TrackedTasksInternal.Add(newTask); + } + + for (var i = 0; i < TrackedTasksInternal.Count; i++) + { + var taskInfo = TrackedTasksInternal[i]; if (taskInfo.Task == null) continue; @@ -81,7 +95,7 @@ namespace Dalamud.Logging.Internal orig(self); var trace = new StackTrace(); - TrackedTasksInternal.Add(new TaskInfo + NewlyCreatedTasks.Enqueue(new TaskInfo { Task = self, Id = self.Id, @@ -114,7 +128,7 @@ namespace Dalamud.Logging.Internal if (patchMethod == null) Log.Error("PatchMethod null!"); - this.scheduleAndStartHook = new Hook(targetMethod, patchMethod); + this.scheduleAndStartHook = new MonoMod.RuntimeDetour.Hook(targetMethod, patchMethod); Log.Information("AddToActiveTasks Hooked!"); }