public void TestProbeCreateClose() { Skip.IfNot(WindowsUtils.IsWindowsNT, reason: "AppMutexes are only available on the Windows NT platform."); string mutexName = "unit-tests-" + Path.GetRandomFileName(); AppMutex.Probe(mutexName).Should().BeFalse(); var mutex = AppMutex.Create(mutexName); AppMutex.Probe(mutexName).Should().BeTrue(); mutex.Close(); AppMutex.Probe(mutexName).Should().BeFalse(); }
public static bool Create([NotNull, Localizable(false)] string name, out AppMutex mutex) { #region Sanity checks if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name)); #endregion if (!WindowsUtils.IsWindowsNT) { mutex = null; return false; } bool existingMutex = false; IntPtr handle1 = IntPtr.Zero, handle2 = IntPtr.Zero; try { if (WindowsMutex.Create("Global\\" + name, out handle1)) { existingMutex = true; Log.Debug("Opened existing global mutex: " + name); } else Log.Debug("Created global mutex: " + name); } #region Error handling catch (Win32Exception ex) { Log.Warn(ex.Message); } #endregion try { if (WindowsMutex.Create(name, out handle2)) { existingMutex = true; Log.Debug("Opened existing local mutex: " + name); } else Log.Debug("Created local mutex: " + name); } #region Error handling catch (Win32Exception ex) { Log.Warn(ex.Message); } #endregion mutex = new AppMutex(new[] {handle1, handle2}); return existingMutex; }