Class ApplicationLock
Represents an application-level lock that can be used to allow only one running instance of the application.
Implements
Inherited Members
Namespace: DotNext.Hosting
Assembly: DotNext.dll
Syntax
public sealed class ApplicationLock : Disposable, IDisposable
Remarks
The lock is based on the exclusively opened lock file in the specified directory. Its scope is the directory: processes that use the same directory compete for the same lock. The lock is released automatically by the operating system when the owning process terminates, even abnormally. The lock file is not deleted when the lock is released. Locking is not reliable if the directory is located on the network file system (NFS or SMB).
Constructors
View SourceApplicationLock(string?)
Initializes a new application-level lock.
Declaration
public ApplicationLock(string? lockFilePath = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | lockFilePath | The directory containing the lock file. If null, BaseDirectory is used. The directory is created if it doesn't exist. |
Remarks
The constructor doesn't acquire the lock. Use TryAcquire() to acquire it.
Exceptions
| Type | Condition |
|---|---|
| UnauthorizedAccessException | The caller doesn't have permission to create the directory. |
| IOException | The directory cannot be created. |
| ArgumentException |
|
Properties
View SourceIsSupported
Gets a value indicating that the application-level lock is supported.
Declaration
public static bool IsSupported { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
View SourceDispose(bool)
Releases managed and unmanaged resources associated with this object.
Declaration
protected override void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | true if called from Dispose(); false if called from finalizer ~Disposable(). |
Overrides
View SourceRelease()
Releases the application lock.
Declaration
public void Release()
Exceptions
| Type | Condition |
|---|---|
| SynchronizationLockException | The lock wasn't acquired by this instance. |
TryAcquire()
Attempts to acquire the lock without blocking.
Declaration
public bool TryAcquire()
Returns
| Type | Description |
|---|---|
| bool | true when the lock is acquired successfully; false if the lock is held by another process or by another ApplicationLock instance, or if it is already held by this instance. |
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | This lock is disposed. |
| UnauthorizedAccessException | The caller doesn't have permission to create or open the lock file. |
| IOException | The lock file cannot be opened for reasons other than the lock conflict. |