diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 00d03882..d5f10871 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -8,7 +8,7 @@ public class SpeedtestService(Config config, Func updateF private readonly Config? _config = config; private readonly Func? _updateFunc = updateFunc; private readonly Lock _runLock = new(); - private CancellationTokenSource? _runCts; + private readonly List _runCtsList = []; private readonly int _speedTestPageSize = config.SpeedTestItem.SpeedTestPageSize ?? Global.SpeedTestPageSize; private readonly TimeSpan _delayInterval = TimeSpan.FromSeconds(config.SpeedTestItem.SpeedTestDelayInterval ?? 1); @@ -18,11 +18,9 @@ public class SpeedtestService(Config config, Func updateF lock (_runLock) { - _runCts?.Cancel(); - runCts = CancellationTokenSource.CreateLinkedTokenSource(ct); - _runCts = runCts; + _runCtsList.Add(runCts); } return RunLoopAsync(actionType, selecteds, runCts); @@ -30,17 +28,30 @@ public class SpeedtestService(Config config, Func updateF public void ExitLoop() { - CancellationTokenSource? runCts; + var counter = 0; + List listToCancel; lock (_runLock) { - runCts = _runCts; + listToCancel = _runCtsList.ToList(); + counter = listToCancel.Count; } - if (runCts is not null) + foreach (var cts in listToCancel) + { + try + { + cts.Cancel(); + } + catch (ObjectDisposedException) + { + // Ignored + } + } + + if (counter > 0) { _ = UpdateFunc("", ResUI.SpeedtestingStop); - runCts.Cancel(); } } @@ -67,10 +78,7 @@ public class SpeedtestService(Config config, Func updateF lock (_runLock) { - if (ReferenceEquals(_runCts, runCts)) - { - _runCts = null; - } + _runCtsList.Remove(runCts); } runCts.Dispose();