diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 6d51f661..2536bf30 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -629,10 +629,12 @@ namespace Netch.Forms private void SettingsButton_Click(object sender, EventArgs e) { + var oldSettings = Global.Settings.Clone(); + Hide(); new SettingForm().ShowDialog(); - if (i18N.LangCode != Global.Settings.Language) + if (oldSettings.Language != Global.Settings.Language) { i18N.Load(Global.Settings.Language); InitText(); @@ -640,10 +642,10 @@ namespace Netch.Forms InitProfile(); } - if (ServerHelper.DelayTestHelper.Interval != Global.Settings.DetectionTick) + if (oldSettings.DetectionTick != Global.Settings.DetectionTick) ServerHelper.DelayTestHelper.UpdateInterval(); - if (ProfileButtons.Count != Global.Settings.ProfileCount) + if (oldSettings.ProfileCount != Global.Settings.ProfileCount) InitProfile(); Show(); diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 929210bb..2006f380 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -73,7 +73,7 @@ namespace Netch.Forms i => Global.Settings.ProfileCount = i, Global.Settings.ProfileCount); BindTextBox(DetectionTickTextBox, - i => i >= 0, + i => ServerHelper.DelayTestHelper.Range.InRange(i), i => Global.Settings.DetectionTick = i, Global.Settings.DetectionTick); BindTextBox(StartedPingIntervalTextBox, diff --git a/Netch/Models/Range.cs b/Netch/Models/Range.cs new file mode 100644 index 00000000..1fdaa3b3 --- /dev/null +++ b/Netch/Models/Range.cs @@ -0,0 +1,20 @@ +namespace Netch.Models +{ + public readonly struct Range + { + public int Start { get; } + + public int End { get; } + + public Range(int start, int end) + { + Start = start; + End = end; + } + + public bool InRange(int num) + { + return Start <= num && num <= End; + } + } +} \ No newline at end of file diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index cb252ef0..97cf3d24 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -286,5 +286,9 @@ namespace Netch.Models public bool UseProxyToUpdateSubscription = false; public V2rayConfig V2RayConfig = new(); + public Setting Clone() + { + return (Setting) MemberwiseClone(); + } } } \ No newline at end of file diff --git a/Netch/Utils/PortHelper.cs b/Netch/Utils/PortHelper.cs index 37bd5637..442657c1 100644 --- a/Netch/Utils/PortHelper.cs +++ b/Netch/Utils/PortHelper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.NetworkInformation; +using Netch.Models; namespace Netch.Utils { @@ -65,7 +66,6 @@ namespace Netch.Utils } } - /// /// 指定类型的端口是否已经被使用了 /// @@ -151,24 +151,6 @@ namespace Netch.Utils } } - internal readonly struct Range - { - public int Start { get; } - - public int End { get; } - - public Range(int start, int end) - { - Start = start; - End = end; - } - - public bool InRange(int num) - { - return Start <= num && num <= End; - } - } - /// /// 检查端口类型 /// diff --git a/Netch/Utils/ServerHelper.cs b/Netch/Utils/ServerHelper.cs index 57e3f3bc..fbedd760 100644 --- a/Netch/Utils/ServerHelper.cs +++ b/Netch/Utils/ServerHelper.cs @@ -23,13 +23,14 @@ namespace Netch.Utils { private static readonly Timer Timer; private static bool _mux; + + public static readonly Range Range = new(0, int.MaxValue / 1000); static DelayTestHelper() { Timer = new Timer { Interval = 10000, - AutoReset = true, - Enabled = false + AutoReset = true }; Timer.Elapsed += (_, _) => TestAllDelay(); @@ -64,17 +65,14 @@ namespace Netch.Utils public static void UpdateInterval() { - var enabled = Enabled; Timer.Stop(); - if (Global.Settings.DetectionTick <= 0) + + if (Global.Settings.DetectionTick == 0 || !Range.InRange(Global.Settings.DetectionTick)) return; Timer.Interval = Global.Settings.DetectionTick * 1000; - if (enabled) - { - Task.Run(TestAllDelay); - Timer.Start(); - } + Task.Run(TestAllDelay); + Timer.Start(); } }