// try acquire lock and then execute the operation // return true if lock acquired and operation executed public static bool TryLockOperation(this IOperationLock lockObj, Action operation, TimeSpan timeout) { var elapsed = TimeSpan.Zero; while (!lockObj.Lock()) { if (elapsed >= timeout) { return(false); } Thread.Sleep(_sleepInterval); elapsed += _sleepInterval; } try { operation(); return(true); } finally { lockObj.Release(); } }
public static async Task <bool> TryLockOperationAsync(this IOperationLock lockObj, Func <Task> operation, TimeSpan timeout) { var interval = TimeSpan.FromMilliseconds(250); var elapsed = TimeSpan.Zero; while (!lockObj.Lock()) { if (elapsed >= timeout) { return(false); } await Task.Delay(_sleepInterval); elapsed += _sleepInterval; } try { await operation(); return(true); } finally { lockObj.Release(); } }
public static async Task<T> LockOperationAsync<T>(this IOperationLock lockObj, Func<Task<T>> operation, string operationName, TimeSpan timeout) { bool isLocked = await WaitToLockAsync(lockObj, operationName, timeout); if (!isLocked) { var lockInfo = lockObj.LockInfo; throw new LockOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, operationName, lockInfo.OperationName, lockInfo.AcquiredDateTime)); } try { return await operation(); } finally { lockObj.Release(); } }
public static async Task <T> LockOperationAsync <T>(this IOperationLock lockObj, Func <Task <T> > operation, TimeSpan timeout) { bool isLocked = await WaitToLockAsync(lockObj, timeout); if (!isLocked) { throw new LockOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, timeout.TotalSeconds)); } try { return(await operation()); } finally { lockObj.Release(); } }
public static async Task <bool> TryLockOperationAsync(this IOperationLock lockObj, Func <Task> operation, TimeSpan timeout) { bool isLocked = await WaitToLockAsync(lockObj, timeout); if (!isLocked) { return(false); } try { await operation(); return(true); } finally { lockObj.Release(); } }
public static void LockOperation(this IOperationLock lockObj, Action operation, Action onBusy) { bool lockTaken = lockObj.Lock(); if (!lockTaken) { onBusy(); return; } try { operation(); } finally { if (lockTaken) { lockObj.Release(); } } }
public void Release() { _lock.Release(); }
public virtual void Release() { _lock.Release(); }