AsyncCrossProcessMutex Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
A mutex that can be entered asynchronously.
public class AsyncCrossProcessMutex : IDisposable
type AsyncCrossProcessMutex = class
interface IDisposable
Public Class AsyncCrossProcessMutex
Implements IDisposable
- Inheritance
-
AsyncCrossProcessMutex
- Implements
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. }
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.
Async |
Initializes a new instance of the AsyncCrossProcessMutex class. |
Name |
Gets the name of the mutex. |
Dispose() |
Disposes of the underlying native objects. |
Enter |
Acquires the mutex asynchronously. |
Enter |
Acquires the mutex asynchronously. |
Try |
Acquires the mutex asynchronously, allowing for timeouts without throwing exceptions. |
Product | Versions |
---|---|
Visual Studio SDK | 2022 |