Beispiel #1
0
        // 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();
            }
        }
Beispiel #2
0
        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();
            }
        }
Beispiel #3
0
        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();
            }
        }
Beispiel #4
0
        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();
            }
        }
Beispiel #5
0
        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();
            }
        }
Beispiel #6
0
        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();
                }
            }
        }
Beispiel #7
0
 public void Release()
 {
     _lock.Release();
 }
Beispiel #8
0
 public virtual void Release()
 {
     _lock.Release();
 }