Optimization and Improvement

Changed callback from synchronous Action<bool, string> to asynchronous Func<bool, string, Task>
This commit is contained in:
2dust 2025-08-29 09:49:30 +08:00
parent abdafc9b3b
commit f83e83de13
3 changed files with 24 additions and 24 deletions

View file

@ -10,11 +10,11 @@ public class CoreAdminManager
private static readonly Lazy<CoreAdminManager> _instance = new(() => new()); private static readonly Lazy<CoreAdminManager> _instance = new(() => new());
public static CoreAdminManager Instance => _instance.Value; public static CoreAdminManager Instance => _instance.Value;
private Config _config; private Config _config;
private Action<bool, string>? _updateFunc; private Func<bool, string, Task>? _updateFunc;
private int _linuxSudoPid = -1; private int _linuxSudoPid = -1;
private const string _tag = "CoreAdminHandler"; private const string _tag = "CoreAdminHandler";
public async Task Init(Config config, Action<bool, string> updateFunc) public async Task Init(Config config, Func<bool, string, Task> updateFunc)
{ {
if (_config != null) if (_config != null)
{ {
@ -26,9 +26,9 @@ public class CoreAdminManager
await Task.CompletedTask; 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<Process?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath) public async Task<Process?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
@ -60,7 +60,7 @@ public class CoreAdminManager
{ {
if (e.Data.IsNotEmpty()) 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)) .WithStandardInputPipe(PipeSource.FromString(AppManager.Instance.LinuxSudoPwd))
.ExecuteBufferedAsync(); .ExecuteBufferedAsync();
UpdateFunc(false, result.StandardOutput.ToString()); await UpdateFunc(false, result.StandardOutput.ToString());
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -14,10 +14,10 @@ public class CoreManager
private Process? _process; private Process? _process;
private Process? _processPre; private Process? _processPre;
private bool _linuxSudo = false; private bool _linuxSudo = false;
private Action<bool, string>? _updateFunc; private Func<bool, string, Task>? _updateFunc;
private const string _tag = "CoreHandler"; private const string _tag = "CoreHandler";
public async Task Init(Config config, Action<bool, string> updateFunc) public async Task Init(Config config, Func<bool, string, Task> updateFunc)
{ {
_config = config; _config = config;
_updateFunc = updateFunc; _updateFunc = updateFunc;
@ -63,7 +63,7 @@ public class CoreManager
{ {
if (node == null) if (node == null)
{ {
UpdateFunc(false, ResUI.CheckServerSettings); await UpdateFunc(false, ResUI.CheckServerSettings);
return; return;
} }
@ -71,13 +71,13 @@ public class CoreManager
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName); var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
if (result.Success != true) if (result.Success != true)
{ {
UpdateFunc(true, result.Msg); await UpdateFunc(true, result.Msg);
return; return;
} }
UpdateFunc(false, $"{node.GetSummary()}"); await UpdateFunc(false, $"{node.GetSummary()}");
UpdateFunc(false, $"{Utils.GetRuntimeInfo()}"); await UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
await CoreStop(); await CoreStop();
await Task.Delay(100); await Task.Delay(100);
@ -91,7 +91,7 @@ public class CoreManager
await CoreStartPreService(node); await CoreStartPreService(node);
if (_process != null) 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 fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false));
var configPath = Utils.GetBinConfigPath(fileName); var configPath = Utils.GetBinConfigPath(fileName);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
UpdateFunc(false, result.Msg); await UpdateFunc(false, result.Msg);
if (result.Success != true) if (result.Success != true)
{ {
return -1; return -1;
} }
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); await UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
UpdateFunc(false, configPath); await UpdateFunc(false, configPath);
var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType); var coreInfo = CoreInfoManager.Instance.GetCoreInfo(coreType);
var proc = await RunProcess(coreInfo, fileName, true, false); 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 #endregion Private
@ -230,7 +230,7 @@ public class CoreManager
var fileName = CoreInfoManager.Instance.GetCoreExecFile(coreInfo, out var msg); var fileName = CoreInfoManager.Instance.GetCoreExecFile(coreInfo, out var msg);
if (fileName.IsNullOrEmpty()) if (fileName.IsNullOrEmpty())
{ {
UpdateFunc(false, msg); await UpdateFunc(false, msg);
return null; return null;
} }
@ -251,7 +251,7 @@ public class CoreManager
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
UpdateFunc(mayNeedSudo, ex.Message); await UpdateFunc(mayNeedSudo, ex.Message);
return null; return null;
} }
} }
@ -284,7 +284,7 @@ public class CoreManager
{ {
if (e.Data.IsNotEmpty()) if (e.Data.IsNotEmpty())
{ {
UpdateFunc(false, e.Data + Environment.NewLine); _ = UpdateFunc(false, e.Data + Environment.NewLine);
} }
} }
proc.OutputDataReceived += dataHandler; proc.OutputDataReceived += dataHandler;

View file

@ -245,7 +245,7 @@ public class MainWindowViewModel : MyReactiveObject
#region Actions #region Actions
private void UpdateHandler(bool notify, string msg) private async Task UpdateHandler(bool notify, string msg)
{ {
NoticeManager.Instance.SendMessage(msg); NoticeManager.Instance.SendMessage(msg);
if (notify) if (notify)
@ -631,7 +631,7 @@ public class MainWindowViewModel : MyReactiveObject
Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu(); Locator.Current.GetService<StatusBarViewModel>()?.RefreshRoutingsMenu();
await ConfigHandler.SaveConfig(_config); await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateHandler); await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler);
await Reload(); await Reload();
} }