From 3e5a4fc1029fb3a427db03dbdbdbf9c4129fdc6e Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Mon, 19 Jul 2021 07:20:04 +0800 Subject: [PATCH] Update NatTestLock --- Netch/Forms/MainForm.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index b5319dec..d4b2346e 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1147,11 +1147,11 @@ namespace Netch.Forms private async void NatTypeStatusLabel_Click(object sender, EventArgs e) { - if (_state == State.Started && !Monitor.IsEntered(_natTestLock)) + if (_state == State.Started && NatTestLock.CurrentCount != 0) await NatTestAsync(); } - private bool _natTestLock = true; + private static readonly SemaphoreSlim NatTestLock = new(1, 1); /// /// 测试 NAT @@ -1161,10 +1161,10 @@ namespace Netch.Forms if (!MainController.Mode!.TestNatRequired()) return; - if (!_natTestLock) + if (NatTestLock.CurrentCount == 0) return; - _natTestLock = false; + await NatTestLock.WaitAsync(); try { @@ -1184,7 +1184,7 @@ namespace Netch.Forms } finally { - _natTestLock = true; + NatTestLock.Release(); } }