Skip to content

Commit

Permalink
Use Tasks instead of Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Rans4ckeR committed Dec 7, 2022
1 parent c1d4eaa commit ce1e90e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
11 changes: 6 additions & 5 deletions ClientCore/SavedGameManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Rampastring.Tools;

namespace ClientCore
Expand All @@ -10,7 +11,7 @@ namespace ClientCore
/// </summary>
public static class SavedGameManager
{
private static bool saveRenameInProgress = false;
private static bool saveRenameInProgress;

public static int GetSaveGameCount()
{
Expand Down Expand Up @@ -88,7 +89,7 @@ public static bool InitSavedGames()
return true;
}

public static void RenameSavedGame()
public static async ValueTask RenameSavedGameAsync()
{
Logger.Log("Renaming saved game.");

Expand Down Expand Up @@ -149,15 +150,15 @@ public static void RenameSavedGame()
return;
}

System.Threading.Thread.Sleep(250);
await Task.Delay(250);
}

saveRenameInProgress = false;

Logger.Log("Saved game SAVEGAME.NET succesfully renamed to " + Path.GetFileName(sgPath));
}

public static bool EraseSavedGames()
private static bool EraseSavedGames()
{
Logger.Log("Erasing previous MP saved games.");

Expand All @@ -178,4 +179,4 @@ public static bool EraseSavedGames()
return true;
}
}
}
}
8 changes: 3 additions & 5 deletions DXMainClient/DXGUI/Multiplayer/GameLoadingLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,14 @@ protected virtual ValueTask LeaveGameAsync()
}

private void fsw_Created(object sender, FileSystemEventArgs e) =>
AddCallback(() => HandleFSWEvent(e));
AddCallback(() => HandleFSWEventAsync(e).HandleTask());

private void HandleFSWEvent(FileSystemEventArgs e)
private static async ValueTask HandleFSWEventAsync(FileSystemEventArgs e)
{
Logger.Log("FSW Event: " + e.FullPath);

if (Path.GetFileName(e.FullPath) == "SAVEGAME.NET")
{
SavedGameManager.RenameSavedGame();
}
await SavedGameManager.RenameSavedGameAsync();
}

private async ValueTask BtnLoadGame_LeftClickAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,9 @@ protected void PostInitialize()
}

private void fsw_Created(object sender, FileSystemEventArgs e)
{
AddCallback(() => FSWEvent(e));
}
=> AddCallback(() => FSWEventAsync(e).HandleTask());

private void FSWEvent(FileSystemEventArgs e)
private async ValueTask FSWEventAsync(FileSystemEventArgs e)
{
Logger.Log("FSW Event: " + e.FullPath);

Expand All @@ -225,7 +223,7 @@ private void FSWEvent(FileSystemEventArgs e)

gameSaved = true;

SavedGameManager.RenameSavedGame();
await SavedGameManager.RenameSavedGameAsync();
}
}

Expand Down

0 comments on commit ce1e90e

Please sign in to comment.