Class SystemLock
Provides a synchronization lock that can be either local (anonymous) or system-wide (named).
Inheritance
Namespace: Umbraco.Cms.Core
Assembly: Umbraco.Core.dll
Syntax
public class SystemLock
Remarks
This is NOT a reader/writer lock and is NOT a recursive lock.
Uses a named Semaphore instead of a Mutex because mutexes have thread affinity which does not work with async situations.
It is important that managed code properly releases the Semaphore before going down, otherwise it will maintain the lock. However, when the whole process (w3wp.exe) goes down and all handles to the Semaphore have been closed, the Semaphore system object is destroyed - so an iisreset should clean up everything.
See https://devblogs.microsoft.com/pfxteam/building-async-coordination-primitives-part-6-asynclock/
Constructors
View SourceSystemLock()
Initializes a new instance of the SystemLock class with an anonymous (local) semaphore.
Declaration
public SystemLock()
SystemLock(string?)
Initializes a new instance of the SystemLock class.
Declaration
public SystemLock(string? name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The name of the system-wide semaphore, or null for an anonymous local semaphore. |
Methods
View SourceLock()
Acquires the lock, blocking until the lock is available.
Declaration
public IDisposable? Lock()
Returns
| Type | Description |
|---|---|
| IDisposable | An System.IDisposable that releases the lock when disposed. |
Lock(int)
Acquires the lock, blocking until the lock is available or the timeout expires.
Declaration
public IDisposable? Lock(int millisecondsTimeout)
Parameters
| Type | Name | Description |
|---|---|---|
| int | millisecondsTimeout | The maximum time in milliseconds to wait for the lock. |
Returns
| Type | Description |
|---|---|
| IDisposable | An System.IDisposable that releases the lock when disposed. |
Exceptions
| Type | Condition |
|---|---|
| TimeoutException | The lock could not be acquired within the timeout period. |