From 0541920469c78330b6a68d5167a540168b096db1 Mon Sep 17 00:00:00 2001 From: ThunderClapLP <59509654+ThunderClapLP@users.noreply.github.com> Date: Tue, 22 Aug 2023 03:00:12 +0200 Subject: [PATCH] fix animation getting stuck on linux from an async function to a timer. I don't know why this fixes it :/ --- MCGet/ConsoleTools/AnimatableTool.cs | 10 ++++++++-- MCGet/ConsoleTools/CToolsEventLoop.cs | 12 +++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/MCGet/ConsoleTools/AnimatableTool.cs b/MCGet/ConsoleTools/AnimatableTool.cs index f21beba..6502cd9 100644 --- a/MCGet/ConsoleTools/AnimatableTool.cs +++ b/MCGet/ConsoleTools/AnimatableTool.cs @@ -12,7 +12,10 @@ public void StartAnimation() if (!CToolsEventLoop.animatableTools.Contains(this)) { animationStarted = true; - CToolsEventLoop.animatableTools.Add(this); + lock (CToolsEventLoop.animatableToolsLock) + { + CToolsEventLoop.animatableTools.Add(this); + } CToolsEventLoop.StartEventLoop(); //start eventloop in case it's not started yet } } @@ -20,7 +23,10 @@ public void StartAnimation() public void StopAnimation() { if (animationStarted) { - CToolsEventLoop.animatableTools.Remove(this); + lock (CToolsEventLoop.animatableToolsLock) + { + CToolsEventLoop.animatableTools.Remove(this); + } } } } diff --git a/MCGet/ConsoleTools/CToolsEventLoop.cs b/MCGet/ConsoleTools/CToolsEventLoop.cs index bd60212..2a6d206 100644 --- a/MCGet/ConsoleTools/CToolsEventLoop.cs +++ b/MCGet/ConsoleTools/CToolsEventLoop.cs @@ -8,28 +8,30 @@ namespace ConsoleTools public class CToolsEventLoop { static bool eventLoopRunning = false; public static List animatableTools = new List(); + public static object animatableToolsLock = new object(); + private static Timer timer = new Timer((object? obj) => { EventLoopTick(); }, null, Timeout.Infinite, Timeout.Infinite); public static void StartEventLoop() { if (eventLoopRunning) return; eventLoopRunning = true; - _ = EventLoopAsync(); + timer.Change(0, 100); } public static void StopEventLoop() { eventLoopRunning = false; + timer.Change(Timeout.Infinite, Timeout.Infinite); } - private static async Task EventLoopAsync() + private static void EventLoopTick() { - while (eventLoopRunning) + lock (animatableToolsLock) { - await Task.Delay(100); foreach (AnimatableTool tool in animatableTools) { - tool.Update(); //not thread safe, but should do for the moment + tool.Update(); } } }