Share via


AsyncCrossProcessMutex Class

Definition

A mutex that can be entered asynchronously.

public class AsyncCrossProcessMutex : IDisposable
type AsyncCrossProcessMutex = class
    interface IDisposable
Public Class AsyncCrossProcessMutex
Implements IDisposable
Inheritance
AsyncCrossProcessMutex
Implements

Examples

using AsyncCrossProcessMutex mutex = new("Some-Unique Name"); using (await mutex.EnterAsync()) { // Code that must not execute in parallel with any other thread or process protected by the same named mutex. }

Remarks

This class utilizes the OS mutex synchronization primitive, which is fundamentally thread-affinitized and requires synchronously blocking the thread that will own the mutex. This makes a native mutex unsuitable for use in async methods, where the thread that enters the mutex may not be the same thread that exits it. This class solves that problem by using a private dedicated thread to enter and release the mutex, but otherwise allows its owner to execute async code, switch threads, etc.

Constructors

AsyncCrossProcessMutex(String)

Initializes a new instance of the AsyncCrossProcessMutex class.

Properties

Name

Gets the name of the mutex.

Methods

Dispose()

Disposes of the underlying native objects.

EnterAsync()

Acquires the mutex asynchronously.

EnterAsync(TimeSpan)

Acquires the mutex asynchronously.

TryEnterAsync(TimeSpan)

Acquires the mutex asynchronously, allowing for timeouts without throwing exceptions.

Applies to