/// <summary> /// Acquires the lock asynchronously, failing with <see cref="TimeoutException"/> if the wait times out /// <code> /// using (await myLock.AcquireAsync(...)) /// { /// // we have the lock /// } /// // dispose releases the lock /// </code> /// </summary> /// <param name="timeout">How long to wait before giving up on acquiring the lock. Defaults to <see cref="Timeout.InfiniteTimeSpan"/></param> /// <param name="cancellationToken">Specifies a token by which the wait can be canceled</param> /// <returns>An <see cref="IDisposable"/> "handle" which can be used to release the lock</returns> public Task <IDisposable> AcquireAsync(TimeSpan?timeout = null, CancellationToken cancellationToken = default(CancellationToken)) { return(DistributedLockHelpers.AcquireAsync(this, timeout, cancellationToken)); }
/// <summary> /// Given <paramref name="baseLockName"/>, constructs a lock name which is safe for use with <see cref="SystemDistributedLock"/> /// </summary> public static string GetSafeLockName(string baseLockName) { return(DistributedLockHelpers.ToSafeLockName(baseLockName, MaxLockNameLength, s => s.Length == 0 ? "EMPTY" : s.Replace('\\', '_'))); }
/// <summary> /// Acquires the lock synchronously, failing with <see cref="TimeoutException"/> if the wait times out /// <code> /// using (myLock.Acquire(...)) /// { /// // we have the lock /// } /// // dispose releases the lock /// </code> /// </summary> /// <param name="timeout">How long to wait before giving up on acquiring the lock. Defaults to <see cref="Timeout.InfiniteTimeSpan"/></param> /// <param name="cancellationToken">Specifies a token by which the wait can be canceled</param> /// <returns>An <see cref="IDisposable"/> "handle" which can be used to release the lock</returns> public IDisposable Acquire(TimeSpan?timeout = null, CancellationToken cancellationToken = default) { return(DistributedLockHelpers.Acquire(this, timeout, cancellationToken)); }