From f83e83de13cb9f9ea5a7b51b4169cbcc9dc60352 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 29 Aug 2025 09:49:30 +0800 Subject: [PATCH] Optimization and Improvement Changed callback from synchronous Action to asynchronous Func --- v2rayN/ServiceLib/Manager/CoreAdminManager.cs | 12 +++---- v2rayN/ServiceLib/Manager/CoreManager.cs | 32 +++++++++---------- .../ViewModels/MainWindowViewModel.cs | 4 +-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/v2rayN/ServiceLib/Manager/CoreAdminManager.cs b/v2rayN/ServiceLib/Manager/CoreAdminManager.cs index 5ba1747b..90b47106 100644 --- a/v2rayN/ServiceLib/Manager/CoreAdminManager.cs +++ b/v2rayN/ServiceLib/Manager/CoreAdminManager.cs @@ -10,11 +10,11 @@ public class CoreAdminManager private static readonly Lazy _instance = new(() => new()); public static CoreAdminManager Instance => _instance.Value; private Config _config; - private Action? _updateFunc; + private Func? _updateFunc; private int _linuxSudoPid = -1; private const string _tag = "CoreAdminHandler"; - public async Task Init(Config config, Action updateFunc) + public async Task Init(Config config, Func updateFunc) { if (_config != null) { @@ -26,9 +26,9 @@ public class CoreAdminManager await Task.CompletedTask; } - private void UpdateFunc(bool notify, string msg) + private async Task UpdateFunc(bool notify, string msg) { - _updateFunc?.Invoke(notify, msg); + await _updateFunc?.Invoke(notify, msg); } public async Task RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath) @@ -60,7 +60,7 @@ public class CoreAdminManager { if (e.Data.IsNotEmpty()) { - UpdateFunc(false, e.Data + Environment.NewLine); + _ = UpdateFunc(false, e.Data + Environment.NewLine); } } @@ -106,7 +106,7 @@ public class CoreAdminManager .WithStandardInputPipe(PipeSource.FromString(AppManager.Instance.LinuxSudoPwd)) .ExecuteBufferedAsync(); - UpdateFunc(false, result.StandardOutput.ToString()); + await UpdateFunc(false, result.StandardOutput.ToString()); } catch (Exception ex) { diff --git a/v2rayN/ServiceLib/Manager/CoreManager.cs b/v2rayN/ServiceLib/Manager/CoreManager.cs index 74cdaf91..695508c2 100644 --- a/v2rayN/ServiceLib/Manager/CoreManager.cs +++ b/v2rayN/ServiceLib/Manager/CoreManager.cs @@ -14,10 +14,10 @@ public class CoreManager private Process? _process; private Process? _processPre; private bool _linuxSudo = false; - private Action? _updateFunc; + private Func? _updateFunc; private const string _tag = "CoreHandler"; - public async Task Init(Config config, Action updateFunc) + public async Task Init(Config config, Func updateFunc) { _config = config; _updateFunc = updateFunc; @@ -63,7 +63,7 @@ public class CoreManager { if (node == null) { - UpdateFunc(false, ResUI.CheckServerSettings); + await UpdateFunc(false, ResUI.CheckServerSettings); return; } @@ -71,13 +71,13 @@ public class CoreManager var result = await CoreConfigHandler.GenerateClientConfig(node, fileName); if (result.Success != true) { - UpdateFunc(true, result.Msg); + await UpdateFunc(true, result.Msg); return; } - UpdateFunc(false, $"{node.GetSummary()}"); - UpdateFunc(false, $"{Utils.GetRuntimeInfo()}"); - UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); + await UpdateFunc(false, $"{node.GetSummary()}"); + await UpdateFunc(false, $"{Utils.GetRuntimeInfo()}"); + await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); await CoreStop(); await Task.Delay(100); @@ -91,7 +91,7 @@ public class CoreManager await CoreStartPreService(node); if (_process != null) { - UpdateFunc(true, $"{node.GetSummary()}"); + await UpdateFunc(true, $"{node.GetSummary()}"); } } @@ -101,14 +101,14 @@ public class CoreManager var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); var configPath = Utils.GetBinConfigPath(fileName); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType); - UpdateFunc(false, result.Msg); + await UpdateFunc(false, result.Msg); if (result.Success != true) { return -1; } - UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); - UpdateFunc(false, configPath); + await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); + await UpdateFunc(false, configPath); var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType); var proc = await RunProcess(coreInfo, fileName, true, false); @@ -216,9 +216,9 @@ public class CoreManager } } - private void UpdateFunc(bool notify, string msg) + private async Task UpdateFunc(bool notify, string msg) { - _updateFunc?.Invoke(notify, msg); + await _updateFunc?.Invoke(notify, msg); } #endregion Private @@ -230,7 +230,7 @@ public class CoreManager var fileName = CoreInfoManager.Instance.GetCoreExecFile(coreInfo, out var msg); if (fileName.IsNullOrEmpty()) { - UpdateFunc(false, msg); + await UpdateFunc(false, msg); return null; } @@ -251,7 +251,7 @@ public class CoreManager catch (Exception ex) { Logging.SaveLog(_tag, ex); - UpdateFunc(mayNeedSudo, ex.Message); + await UpdateFunc(mayNeedSudo, ex.Message); return null; } } @@ -284,7 +284,7 @@ public class CoreManager { if (e.Data.IsNotEmpty()) { - UpdateFunc(false, e.Data + Environment.NewLine); + _ = UpdateFunc(false, e.Data + Environment.NewLine); } } proc.OutputDataReceived += dataHandler; diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index c297ffc5..f272b9f9 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -245,7 +245,7 @@ public class MainWindowViewModel : MyReactiveObject #region Actions - private void UpdateHandler(bool notify, string msg) + private async Task UpdateHandler(bool notify, string msg) { NoticeManager.Instance.SendMessage(msg); if (notify) @@ -631,7 +631,7 @@ public class MainWindowViewModel : MyReactiveObject Locator.Current.GetService()?.RefreshRoutingsMenu(); await ConfigHandler.SaveConfig(_config); - await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler); + await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler); await Reload(); }