diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index b0749e8b..3e48e62c 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using Microsoft.VisualStudio.Threading; using Netch.Interfaces; using Netch.Models; using Netch.Utils; diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index cef69a8a..8b3026cd 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -12,6 +12,7 @@ using System.Windows.Forms; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.UI.WindowsAndMessaging; +using Microsoft.VisualStudio.Threading; using Microsoft.Win32; using Netch.Controllers; using Netch.Enums; @@ -518,7 +519,7 @@ namespace Netch.Forms return; } - await Configuration.SaveAsync(); + Configuration.SaveAsync().Forget(); // 服务器、模式 需选择 if (ServerComboBox.SelectedItem is not Server server) diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index c013981d..c79bf4c6 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -10,6 +10,7 @@ false latest enable + VSTHRD002;VSTHRD200;VSTHRD100;VSTHRD101;VSTHRD110;VSTHRD130 false Default true @@ -39,6 +40,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Netch/Utils/Configuration.cs b/Netch/Utils/Configuration.cs index 40bf23d4..32464e83 100644 --- a/Netch/Utils/Configuration.cs +++ b/Netch/Utils/Configuration.cs @@ -3,7 +3,9 @@ using System.IO; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; +using System.Threading; using System.Threading.Tasks; +using Microsoft.VisualStudio.Threading; using Netch.Models; using Serilog; @@ -24,6 +26,8 @@ namespace Netch.Utils private const string BackupFileName = "settings.json.bak"; + private static readonly AsyncReaderWriterLock _lock = new(null); + private static readonly JsonSerializerOptions JsonSerializerOptions = Global.NewCustomJsonSerializerOptions(); static Configuration() @@ -93,8 +97,14 @@ namespace Netch.Utils /// public static async Task SaveAsync() { + if (_lock.IsWriteLockHeld) + return; + try { + await using var _ = await _lock.WriteLockAsync(); + Log.Verbose("Save Configuration"); + if (!Directory.Exists(DataDirectoryFullName)) Directory.CreateDirectory(DataDirectoryFullName); diff --git a/Netch/Utils/TplExtensions.cs b/Netch/Utils/TplExtensions.cs deleted file mode 100644 index a11d6ddc..00000000 --- a/Netch/Utils/TplExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Threading.Tasks; - -namespace Netch.Utils -{ - public static class TplExtensions - { - public static void Forget(this Task? task) - { - } - } -} \ No newline at end of file diff --git a/Netch/Utils/WebUtil.cs b/Netch/Utils/WebUtil.cs index 10e50238..458ffdea 100644 --- a/Netch/Utils/WebUtil.cs +++ b/Netch/Utils/WebUtil.cs @@ -3,6 +3,7 @@ using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; +using Microsoft.VisualStudio.Threading; namespace Netch.Utils {