Show / Hide Table of Contents

Class ApplicationLock

Represents an application-level lock that can be used to allow only one running instance of the application.

Inheritance
object
Disposable
ApplicationLock
Implements
IDisposable
Inherited Members
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(params IEnumerable<IAsyncDisposable>)
Disposable.Dispose<TDisposable>(params ReadOnlySpan<TDisposable>)
object.GetType()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
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 Source

ApplicationLock(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

lockFilePath doesn't contain a directory name.

Properties

View Source

IsSupported

Gets a value indicating that the application-level lock is supported.

Declaration
public static bool IsSupported { get; }
Property Value
Type Description
bool

Methods

View Source

Dispose(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
Disposable.Dispose(bool)
View Source

Release()

Releases the application lock.

Declaration
public void Release()
Exceptions
Type Condition
SynchronizationLockException

The lock wasn't acquired by this instance.

View Source

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.

Implements

IDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾