From 5bef6b0492b27d65ec438e77378f2818c5e97320 Mon Sep 17 00:00:00 2001 From: Tsuki Date: Sat, 1 Apr 2023 18:57:38 +0800 Subject: [PATCH] fix some bug --- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 80 +++++++++++++------ .../v2rayN/ViewModels/MainWindowViewModel.cs | 12 ++- 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index 8e22454e..d5817d89 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -137,7 +137,8 @@ namespace v2rayN.Handler UpdateFunc(it.indexId, output); }); } - + + private readonly object _lock = new object(); private Task RunRealPing() { int pid = -1; @@ -174,13 +175,20 @@ namespace v2rayN.Handler string output = GetRealPingTime(downloadHandle, webProxy); ProfileExHandler.Instance.SetTestDelay(it.indexId, output); - UpdateFunc(it.indexId, output); - int.TryParse(output, out int delay); - it.delay = delay; + // add a lock to synchronize access to _selecteds list + lock (_lock){ + UpdateFunc(it.indexId, output); + int.TryParse(output, out int delay); + it.delay = delay; + } } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); + // remove the failed item from the list + lock (_lock){ + _selecteds.Remove(it); + } } })); //Thread.Sleep(100); @@ -219,17 +227,22 @@ namespace v2rayN.Handler var timeout = _config.speedTestItem.speedTestTimeout; DownloadHandle downloadHandle = new(); + + // add a lock to synchronize access to _selecteds list + lock (_lock){ + _selecteds = _selecteds.Where(it => it.allowTest && it.configType != EConfigType.Custom).ToList(); + } foreach (var it in _selecteds) { - if (!it.allowTest) - { - continue; - } - if (it.configType == EConfigType.Custom) - { - continue; - } + // if (!it.allowTest) + // { + // continue; + // } + // if (it.configType == EConfigType.Custom) + // { + // continue; + // } //if (it.delay < 0) //{ // UpdateFunc(it.indexId, "", ResUI.SpeedtestingSkip); @@ -243,15 +256,21 @@ namespace v2rayN.Handler WebProxy webProxy = new(Global.Loopback, it.port); - await downloadHandle.DownloadDataAsync(url, webProxy, timeout, async (bool success, string msg) => - { + try{ + var msg = await downloadHandle.DownloadDataAsync(url, webProxy, timeout); decimal.TryParse(msg, out decimal dec); if (dec > 0) { ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg); } UpdateFunc(it.indexId, "", msg); - }); + } + catch (Exception ex){ + Utils.SaveLog(ex.Message, ex); + lock (_lock){ + _selecteds.RemoveAll(s => s.indexId == it.indexId); + } + } } if (pid > 0) @@ -276,7 +295,9 @@ namespace v2rayN.Handler var timeout = _config.speedTestItem.speedTestTimeout; DownloadHandle downloadHandle = new(); - + + // add a lock to synchronize access to _selecteds list + var _lock = new object(); foreach (var it in _selecteds) { if (!it.allowTest) @@ -294,16 +315,23 @@ namespace v2rayN.Handler if (item is null) continue; WebProxy webProxy = new(Global.Loopback, it.port); - _ = downloadHandle.DownloadDataAsync(url, webProxy, timeout, async (bool success, string msg) => - { - decimal.TryParse(msg, out decimal dec); - if (dec > 0) - { - ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg); - } - UpdateFunc(it.indexId, "", msg); - }); - Thread.Sleep(2000); + try{ + await downloadHandle.DownloadDataAsync(url, webProxy, timeout, async (bool success, string msg) =>{ + decimal.TryParse(msg, out decimal dec); + if (dec > 0) + { + ProfileExHandler.Instance.SetTestSpeed(it.indexId, msg); + } + UpdateFunc(it.indexId, "", msg); + }); + } + catch (Exception ex){ + // remove the failed item from the list + lock (_lock){ + _selecteds.Remove(it); + } + UpdateFunc(it.indexId, "", ResUI.SpeedtestingFailed + ": " + ex.Message); + } } Thread.Sleep((timeout + 2) * 1000); diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index 86255d79..0f7a89ac 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -563,8 +563,8 @@ namespace v2rayN.ViewModels { return; } - SpeedProxyDisplay = string.Format("{0}:{1}/s¡ü | {2}/s¡ý", Global.agentTag, Utils.HumanFy(update.proxyUp), Utils.HumanFy(update.proxyDown)); - SpeedDirectDisplay = string.Format("{0}:{1}/s¡ü | {2}/s¡ý", Global.directTag, Utils.HumanFy(update.directUp), Utils.HumanFy(update.directDown)); + SpeedProxyDisplay = string.Format("{0}:{1}/s�� | {2}/s��", Global.agentTag, Utils.HumanFy(update.proxyUp), Utils.HumanFy(update.proxyDown)); + SpeedDirectDisplay = string.Format("{0}:{1}/s�� | {2}/s��", Global.directTag, Utils.HumanFy(update.directUp), Utils.HumanFy(update.directDown)); if (update.proxyUp + update.proxyDown > 0) { @@ -1715,6 +1715,14 @@ namespace v2rayN.ViewModels public void ModifyTheme(bool isDarkTheme) { var theme = _paletteHelper.GetTheme(); + //add follow systemTheme + var systemTheme = App.Current.RequestedTheme; + if (systemTheme == AppTheme.Dark){ + isDarkTheme = true; + } + else if (systemTheme == AppTheme.Light){ + isDarkTheme = false; + } theme.SetBaseTheme(isDarkTheme ? Theme.Dark : Theme.Light); _paletteHelper.SetTheme(theme);