From 8aeee1e23886ff5c338dc8716722fa0f76761e10 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 26 Nov 2020 18:22:55 +0100 Subject: [PATCH] Allow retry in mutex acquire process --- ArchiSteamFarm/OS.cs | 18 +++++++++++++++--- ArchiSteamFarm/Program.cs | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ArchiSteamFarm/OS.cs b/ArchiSteamFarm/OS.cs index 68bc1c602..ebc5639c7 100644 --- a/ArchiSteamFarm/OS.cs +++ b/ArchiSteamFarm/OS.cs @@ -28,6 +28,7 @@ using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; +using System.Threading.Tasks; using ArchiSteamFarm.Helpers; using ArchiSteamFarm.Localization; @@ -92,7 +93,7 @@ namespace ArchiSteamFarm { } } - internal static bool RegisterProcess() { + internal static async Task RegisterProcess() { if (SingleInstance != null) { return false; } @@ -106,11 +107,22 @@ namespace ArchiSteamFarm { uniqueName = "Global\\" + GetOsResourceName(nameof(SingleInstance)) + "-" + BitConverter.ToString(hashingAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(Directory.GetCurrentDirectory()))).Replace("-", ""); } - Mutex singleInstance = new(true, uniqueName, out bool result); + Mutex? singleInstance = null; + + for (byte i = 0; (i < WebBrowser.MaxTries) && (singleInstance == null); i++) { + singleInstance = new Mutex(true, uniqueName, out bool result); + + if (result) { + break; + } - if (!result) { singleInstance.Dispose(); + singleInstance = null; + await Task.Delay(1000).ConfigureAwait(false); + } + + if (singleInstance == null) { return false; } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 1864121a1..e60c04515 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -170,7 +170,8 @@ namespace ArchiSteamFarm { ParseArgs(args); } - bool uniqueInstance = OS.RegisterProcess(); + bool uniqueInstance = await OS.RegisterProcess().ConfigureAwait(false); + Logging.InitCoreLoggers(uniqueInstance); if (!uniqueInstance) {