diff --git a/KfChatDotNetKickBot/KickBot.cs b/KfChatDotNetKickBot/KickBot.cs index 2a733e0..e32d16f 100644 --- a/KfChatDotNetKickBot/KickBot.cs +++ b/KfChatDotNetKickBot/KickBot.cs @@ -114,6 +114,7 @@ public class KickBot { if (e.Type == DisconnectionType.ByServer) { + _shuffle.Dispose(); BuildShuffle(); } } diff --git a/KfChatDotNetKickBot/Services/Shuffle.cs b/KfChatDotNetKickBot/Services/Shuffle.cs index a7e9317..0c62e22 100644 --- a/KfChatDotNetKickBot/Services/Shuffle.cs +++ b/KfChatDotNetKickBot/Services/Shuffle.cs @@ -8,7 +8,7 @@ using Websocket.Client; namespace KfChatDotNetKickBot.Services; -public class Shuffle +public class Shuffle : IDisposable { private Logger _logger = LogManager.GetCurrentClassLogger(); private WebsocketClient _wsClient; @@ -20,6 +20,7 @@ public class Shuffle public event OnLatestBetUpdatedEventHandler OnLatestBetUpdated; public event OnWsDisconnectionEventHandler OnWsDisconnection; private CancellationToken _cancellationToken = CancellationToken.None; + private CancellationTokenSource _pingCts = new(); public Shuffle(string? proxy = null, CancellationToken? cancellationToken = null) { @@ -79,7 +80,7 @@ public class Shuffle private async Task PeriodicPing() { using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10)); - while (await timer.WaitForNextTickAsync(_cancellationToken)) + while (await timer.WaitForNextTickAsync(_pingCts.Token)) { if (_wsClient == null) { @@ -217,4 +218,11 @@ public class Shuffle } public class ShuffleUserNotFoundException : Exception; + + public void Dispose() + { + _pingCts.Cancel(); + _wsClient.Dispose(); + GC.SuppressFinalize(this); + } } \ No newline at end of file