From cbc6822bffcba783a24c554e94b6a7eb6af081cf Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Tue, 14 Sep 2021 08:49:37 +0800 Subject: [PATCH] Refactor Cancel Connectivity Test --- Netch/Forms/MainForm.cs | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 16b10da5..a35295ac 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1058,6 +1058,8 @@ namespace Netch.Forms private async Task StopCoreAsync() { State = State.Stopping; + _discoveryNatCts?.Cancel(); + _httpConnectCts?.Cancel(); await MainController.StopAsync(); State = State.Stopped; } @@ -1082,9 +1084,10 @@ namespace Netch.Forms } text ??= i18N.Translate(StateExtension.GetStatusString(State)); - StatusLabel.Text = i18N.Translate("Status", ": ") + text; if (_state == State.Started) - StatusLabel.Text += StatusPortInfoText.Value; + text += StatusPortInfoText.Value; + + StatusLabel.Text = i18N.Translate("Status", ": ") + text; } public void BandwidthState(bool state) @@ -1164,19 +1167,20 @@ namespace Netch.Forms await DiscoveryNatTypeAsync(); } + private CancellationTokenSource? _discoveryNatCts; + private async Task DiscoveryNatTypeAsync() { NatTypeStatusLabel.Enabled = false; - NatTypeStatusLabel.Text = i18N.Translate("Testing NAT Type"); + UpdateNatTypeStatusLabelText("Testing NAT Type"); - using var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromSeconds(5)); - - var discoveryNatTypeAsync = MainController.DiscoveryNatTypeAsync(cts.Token); + _discoveryNatCts = new CancellationTokenSource(); try { - var res = await discoveryNatTypeAsync; + var res = await MainController.DiscoveryNatTypeAsync(_discoveryNatCts.Token); + if (_discoveryNatCts.IsCancellationRequested) + return; if (!string.IsNullOrEmpty(res.PublicEnd)) { @@ -1196,23 +1200,28 @@ namespace Netch.Forms } finally { + _discoveryNatCts.Dispose(); + _discoveryNatCts = null; NatTypeStatusLabel.Enabled = true; } } + private CancellationTokenSource? _httpConnectCts; + private async Task HttpConnectAsync() { TcpStatusLabel.Enabled = false; - using var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromSeconds(5)); - var httpConnectAsync = MainController.HttpConnectAsync(cts.Token); + _httpConnectCts = new CancellationTokenSource(); try { - var httpRes = await httpConnectAsync; - if (httpRes != null) - TcpStatusLabel.Text = $"TCP{i18N.Translate(": ")}{httpRes}ms"; + var res = await MainController.HttpConnectAsync(_httpConnectCts.Token); + if (_httpConnectCts.IsCancellationRequested) + return; + + if (res != null) + TcpStatusLabel.Text = $"TCP{i18N.Translate(": ")}{res}ms"; else TcpStatusLabel.Text = $"TCP{i18N.Translate(": ", "Timeout")}"; @@ -1220,6 +1229,8 @@ namespace Netch.Forms } finally { + _httpConnectCts.Dispose(); + _httpConnectCts = null; TcpStatusLabel.Enabled = true; } }