using Nito.AsyncEx;
using WalletWasabi.Crypto;

namespace WalletWasabi.Io
{
	public class MutexIoManager : IoManager
	{
		public MutexIoManager(string filePath) : base(filePath)
		{
			var shortHash = HashHelpers.GenerateSha256Hash(FilePath).Substring(1, 7);

			// https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=netframework-4.8
			// On a server that is running Terminal Services, a named system mutex can have two levels of visibility.
			// If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions.
			// If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created.
			// In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server.
			// If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\".
			// Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes,
			// and both are visible to all processes in the terminal server session.
			// That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.
			Mutex = new AsyncMutex($"{FileNameWithoutExtension}-{shortHash}");
		}

		public AsyncMutex Mutex { get; }
	}
}

using Nito.AsyncEx;
using WalletWasabi.Crypto;

namespace WalletWasabi.Io
{
	public class MutexIoManager : IoManager
	{
		public MutexIoManager(string filePath) : base(filePath)
		{
			var shortHash = HashHelpers.GenerateSha256Hash(FilePath).Substring(0, 7);

			// https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=netframework-4.8
			// On a server that is running Terminal Services, a named system mutex can have two levels of visibility.
			// If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions.
			// If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created.
			// In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server.
			// If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\".
			// Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes,
			// and both are visible to all processes in the terminal server session.
			// That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.
			Mutex = new AsyncMutex($"{FileNameWithoutExtension}-{shortHash}");
		}

		public AsyncMutex Mutex { get; }
	}
}

Fix off by one bug
Fix off by one bug
