Skip to content

Commit

Permalink
fix animation getting stuck on linux
Browse files Browse the repository at this point in the history
from an async function to a timer.
I don't know why this fixes it :/
  • Loading branch information
ThunderClapLP committed Aug 22, 2023
1 parent defe399 commit 0541920
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
10 changes: 8 additions & 2 deletions MCGet/ConsoleTools/AnimatableTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ 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
}
}

public void StopAnimation()
{
if (animationStarted) {
CToolsEventLoop.animatableTools.Remove(this);
lock (CToolsEventLoop.animatableToolsLock)
{
CToolsEventLoop.animatableTools.Remove(this);
}
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions MCGet/ConsoleTools/CToolsEventLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ namespace ConsoleTools
public class CToolsEventLoop {
static bool eventLoopRunning = false;
public static List<AnimatableTool> animatableTools = new List<AnimatableTool>();
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();
}
}
}
Expand Down

0 comments on commit 0541920

Please sign in to comment.