Exemple #1
0
        public ICommand AsyncCommand <TParam>(Func <TParam, Task> execute, Func <TParam, bool> canExecute = null)
        {
            Func <TParam, Task> func = async param =>
            {
                if (IsLocked)
                {
                    return;
                }

                long currentLockIndex = 0;
                try
                {
                    if (!_commandExecutionLock.TryLockExecution())
                    {
                        return;
                    }

                    currentLockIndex = Interlocked.Increment(ref _lockIndex);
                    try
                    {
                        await execute(param);
                    }
                    catch (Exception e)
                    {
                        if (!HandleException(e))
                        {
                            throw;
                        }
                    }
                }
                finally
                {
                    if (Interlocked.Read(ref _lockIndex) == currentLockIndex)
                    {
                        _commandExecutionLock.FreeExecutionLock();
                    }
                }
            };

            return(new Command <TParam>(o => func(o), par => CanExecute(par, canExecute)));
        }
Exemple #2
0
        public IAsyncCommand AsyncCommand(
            Func <Task> execute, Func <bool> canExecute = null)
        {
            async Task Func()
            {
                if (IsLocked)
                {
                    return;
                }

                long currentLockIndex = 0;

                try
                {
                    if (!_commandExecutionLock.TryLockExecution())
                    {
                        return;
                    }

                    currentLockIndex = Interlocked.Increment(ref _lockIndex);
                    try
                    {
                        await execute();
                    }
                    catch (Exception e)
                    {
                        if (!HandleException(e))
                        {
                            throw;
                        }
                    }
                }
                finally
                {
                    if (Interlocked.Read(ref _lockIndex) == currentLockIndex)
                    {
                        _commandExecutionLock.FreeExecutionLock();
                    }
                }
            }

            return(new AsyncCommand(Func, canExecute));
        }