From 86b1741dd0685e7d7638af29fa013ef64a54d74a Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 28 May 2021 14:51:30 +0800 Subject: [PATCH] Revert NatTest Monitor lock --- Netch/Controllers/NTTController.cs | 2 +- Netch/Forms/MainForm.cs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs index b05b571d..91e6be18 100644 --- a/Netch/Controllers/NTTController.cs +++ b/Netch/Controllers/NTTController.cs @@ -36,7 +36,7 @@ namespace Netch.Controllers try { - File.WriteAllText(Path.Combine(Global.NetchDir, $"logging\\{Name}.log"), $"{output}\r\n{error}"); + await File.WriteAllTextAsync(Path.Combine(Global.NetchDir, $"logging\\{Name}.log"), $"{output}\r\n{error}"); } catch (Exception e) { diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 3c06e980..ad62def9 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1110,7 +1110,7 @@ namespace Netch.Forms await NatTest(); } - private readonly object _natTestLock = new(); + private bool _natTestLock = true; /// /// 测试 NAT @@ -1120,13 +1120,16 @@ namespace Netch.Forms if (!MainController.Mode!.TestNatRequired()) return; - if (!Monitor.TryEnter(_natTestLock)) + if (!_natTestLock) return; + _natTestLock = false; + try { NatTypeStatusText(i18N.Translate("Starting NatTester")); + // Monitor.TryEnter() Monitor.Exit() (a.k.a. lock) not work with async/await var (result, _, publicEnd) = await MainController.NTTController.Start(); if (!string.IsNullOrEmpty(publicEnd)) @@ -1141,7 +1144,7 @@ namespace Netch.Forms } finally { - Monitor.Exit(_natTestLock); + _natTestLock = true; } }