From 29f99ceacc3a60d1e1d8a043ec94fb27bba896f4 Mon Sep 17 00:00:00 2001 From: DHR60 <192860629+DHR60@users.noreply.github.com> Date: Sat, 26 Sep 2026 03:10:30 +0000 Subject: [PATCH] Fix speed test (#10219) --- .../ServiceLib/Services/SpeedtestService.cs | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) 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();