From 201cfaa922de7f297e60cea5eb2332454c71f8a8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 2 Nov 2024 15:02:30 +0800 Subject: [PATCH] Fix https://github.com/2dust/v2rayN/issues/5966 --- v2rayN/ServiceLib/Enums/EMsgCommand.cs | 3 ++- v2rayN/ServiceLib/Services/SpeedtestService.cs | 8 +++++--- v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/v2rayN/ServiceLib/Enums/EMsgCommand.cs b/v2rayN/ServiceLib/Enums/EMsgCommand.cs index 31d7556f..c955e830 100644 --- a/v2rayN/ServiceLib/Enums/EMsgCommand.cs +++ b/v2rayN/ServiceLib/Enums/EMsgCommand.cs @@ -5,6 +5,7 @@ ClearMsg, SendMsgView, SendSnackMsg, - RefreshProfiles + RefreshProfiles, + StopSpeedtest } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 61bfd7c1..d8411903 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -1,7 +1,7 @@ -using System.Diagnostics; +using ReactiveUI; +using System.Diagnostics; using System.Net; using System.Net.Sockets; - namespace ServiceLib.Services { public class SpeedtestService @@ -80,10 +80,12 @@ namespace ServiceLib.Services Task.Run(RunMixedtestAsync); break; } + MessageBus.Current.Listen(EMsgCommand.StopSpeedtest.ToString()).Subscribe(ExitLoop); } - public void ExitLoop() + private void ExitLoop(string x) { + if(_exitLoop) return; _exitLoop = true; UpdateFunc("", ResUI.SpeedtestingStop); } diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index 8249d6e8..66d38ddc 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -16,7 +16,6 @@ namespace ServiceLib.ViewModels private List _lstProfile; private string _serverFilter = string.Empty; private Dictionary _dicHeaderSort = new(); - private SpeedtestService? _speedtestHandler; #endregion private prop @@ -686,12 +685,13 @@ namespace ServiceLib.ViewModels } //ClearTestResult(); - _speedtestHandler = new SpeedtestService(_config, lstSelecteds, actionType, UpdateSpeedtestHandler); + _ = new SpeedtestService(_config, lstSelecteds, actionType, UpdateSpeedtestHandler); + } public void ServerSpeedtestStop() { - _speedtestHandler?.ExitLoop(); + MessageBus.Current.SendMessage("", EMsgCommand.StopSpeedtest.ToString()); } private async Task Export2ClientConfigAsync(bool blClipboard)