Fix speed test (#10219)

This commit is contained in:
DHR60
2026-09-26 03:10:30 +00:00
committed by GitHub
parent 93d8174dbe
commit 29f99ceacc
+20 -12
View File
@@ -8,7 +8,7 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
private readonly Config? _config = config; private readonly Config? _config = config;
private readonly Func<SpeedTestResult, Task>? _updateFunc = updateFunc; private readonly Func<SpeedTestResult, Task>? _updateFunc = updateFunc;
private readonly Lock _runLock = new(); private readonly Lock _runLock = new();
private CancellationTokenSource? _runCts; private readonly List<CancellationTokenSource> _runCtsList = [];
private readonly int _speedTestPageSize = config.SpeedTestItem.SpeedTestPageSize ?? Global.SpeedTestPageSize; private readonly int _speedTestPageSize = config.SpeedTestItem.SpeedTestPageSize ?? Global.SpeedTestPageSize;
private readonly TimeSpan _delayInterval = TimeSpan.FromSeconds(config.SpeedTestItem.SpeedTestDelayInterval ?? 1); private readonly TimeSpan _delayInterval = TimeSpan.FromSeconds(config.SpeedTestItem.SpeedTestDelayInterval ?? 1);
@@ -18,11 +18,9 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
lock (_runLock) lock (_runLock)
{ {
_runCts?.Cancel();
runCts = CancellationTokenSource.CreateLinkedTokenSource(ct); runCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
_runCts = runCts; _runCtsList.Add(runCts);
} }
return RunLoopAsync(actionType, selecteds, runCts); return RunLoopAsync(actionType, selecteds, runCts);
@@ -30,17 +28,30 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
public void ExitLoop() public void ExitLoop()
{ {
CancellationTokenSource? runCts; var counter = 0;
List<CancellationTokenSource> listToCancel;
lock (_runLock) 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); _ = UpdateFunc("", ResUI.SpeedtestingStop);
runCts.Cancel();
} }
} }
@@ -67,10 +78,7 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
lock (_runLock) lock (_runLock)
{ {
if (ReferenceEquals(_runCts, runCts)) _runCtsList.Remove(runCts);
{
_runCts = null;
}
} }
runCts.Dispose(); runCts.Dispose();