Allow retry in mutex acquire process

This commit is contained in:
JustArchi
2020-11-26 18:22:55 +01:00
parent 0a25ee9197
commit 8aeee1e238
2 changed files with 17 additions and 4 deletions

View File

@@ -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<bool> 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;
}

View File

@@ -170,7 +170,8 @@ namespace ArchiSteamFarm {
ParseArgs(args);
}
bool uniqueInstance = OS.RegisterProcess();
bool uniqueInstance = await OS.RegisterProcess().ConfigureAwait(false);
Logging.InitCoreLoggers(uniqueInstance);
if (!uniqueInstance) {