MiniMutex
MiniMutex is a lightweight, zero-dependency helper for safely claiming named mutexes.
Use it to gate, block, or unlock single-instance apps and coordinated worker processes.
Targets .NET 6+.
🚀 Features
- Single-instance application enforcement
- Named mutex claiming with configurable behavior
- Optional waiting (block, timeout, or no-wait)
- Global namespaces for cross-session mutexes
- Robust abandoned mutex handling
- Disposable mutex handles
- Manager for tracking multiple claims
- Safe defaults with
MiniMutexGate.Init()
📦 Installation
dotnet add package MiniMutex
Or via the NuGet package manager:
Install-Package MiniMutex
🧭 Usage
Claim a mutex
using MiniMutex;
if (MiniMutexGate.TryClaim("my-app-single-instance",
out var handle,
new MiniMutexOptions { UseGlobalNamespace = true }))
{
handle.Dispose();
}
Manage multiple claims together
using var manager = new MiniMutexManager();
if (manager.TryClaim("worker-1", out _)
&& manager.TryClaim("worker-2", out _))
{
}
manager.ReleaseAll();
⚙️ Set reusable defaults
MiniMutexGate.Init(new MiniMutexOptions
{
UseGlobalNamespace = true,
WaitForExisting = TimeSpan.FromSeconds(2),
});
MiniMutexGate.TryClaim("shared-job", out _);
🔧 Options Overview
MiniMutexOptions lets you configure:
- InitiallyOwned – own immediately on creation
- WaitForExisting – timeout for existing locks
- UseGlobalNamespace – cross-session mutex support
- AllowAbandoned – treat abandoned mutexes as success
- ReleaseOnDispose – auto-release on
Dispose()
- ExitContext – call WaitOne with synchronization context exit
📜 License
MiniMutex is licensed under the MIT License.