Exemplo n.º 1
0
        // This execute is used, if the command itself is a CommandRegistry that has IAsyncCommands
        public virtual void Execute(Object parameter, INextCommandDelegate commandFinishedCallback)
        {
            if (!this.CanExecute())
            {
                return;
            }
            lock (busyLock)
            {
                if (busyCount > 0)
                {
                    if (!AllowConcurrentExecution)
                    {
                        // The registry is already busy and concurrent execution is not allowed -> we are finished here:
                        if (commandFinishedCallback != null)
                        {
                            commandFinishedCallback.Invoke(false);
                        }
                        return;
                    }
                }
                else
                {
                    GuiThreadHelper.InvokeInGuiAndWait(delegate()
                    {
                        IncreaseBusyCount();
                    });
                }
            }

            IList <ICommandContainer> commandContainers = GetOrderedCommandContainers();

            CommandRegistryFinishedCallback registryFinishedCallback = new CommandRegistryFinishedCallback(delegate(bool success)
            {
                DecreaseBusyCount();
                RaiseCanExecuteChanged();
                if (commandFinishedCallback != null)
                {
                    commandFinishedCallback.Invoke(success);
                }
            });

            if (commandContainers.Count < 1)
            {
                registryFinishedCallback(true);
                return;
            }
            Execute(commandContainers, false, registryFinishedCallback);
        }
Exemplo n.º 2
0
 protected virtual void AsyncExecuteRecursion(IList <ICommandContainer> commandChain, long processSequenceId, bool success, bool onlyIFinallyCommands, CommandRegistryFinishedCallback registryFinishedCallback)
 {
     FreeCommand(processSequenceId);
     if (!success)
     {
         Log.Warn("Execution of the async command with Id " + processSequenceId + " was not successful (" + commandChain[0] + ")");
         onlyIFinallyCommands = true;
     }
     // With asychronous execution we have left the GUI thread, so we have to return
     // with the next command:
     GuiThreadHelper.InvokeInGui(delegate()
     {
         ExecuteRecursion(commandChain, onlyIFinallyCommands, registryFinishedCallback);
     });
 }
Exemplo n.º 3
0
        protected virtual void ExecuteRecursion(IList <ICommandContainer> commandChain, bool onlyIFinallyCommands, CommandRegistryFinishedCallback registryFinishedCallback)
        {
            IList <ICommandContainer> commandChainCpy = new List <ICommandContainer>(commandChain);

            commandChainCpy.RemoveAt(0);
            if (commandChainCpy.Count > 0)
            {
                Execute(commandChainCpy, onlyIFinallyCommands, registryFinishedCallback);
            }
            else
            {
                // Excecution of the command chain is finished
                if (registryFinishedCallback != null)
                {
                    // This callback is used, if the registry itself is used as an IAsyncCommand within another command registry
                    // New: It is generally used to call DecreasyBusyCount()
                    registryFinishedCallback.Invoke(!onlyIFinallyCommands);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes a CommandChain from the beginning.
        /// </summary>
        /// <param name="commandChain"></param>
        /// <param name="onlyIFinallyCommands">indicates errors, if true only finally commands are executed. If an finally command throws an exceptions, remaining finally commands are still executed.</param>
        protected virtual void Execute(IList <ICommandContainer> commandChain, bool onlyIFinallyCommands, CommandRegistryFinishedCallback registryFinishedCallback = null)
        {
            if (commandChain.Count < 1)
            {
                String error = "It is not allowed to call execute with a command-chain of size zero.";
                Log.Error(error);
                throw new ArgumentException(error);
            }
            ICommandContainer commandContainer = commandChain[0];

            if (onlyIFinallyCommands)
            {
                if (!(commandContainer.Command is IFinallyCommand))
                {
                    ExecuteRecursion(commandChain, onlyIFinallyCommands, registryFinishedCallback);
                    return;
                }
            }
            ICommand command          = commandContainer.Command;
            Object   commandParameter = commandContainer.CommandParameter;

            if ((command is IAsyncCommand) || ((command is CommandRegistry) && ((CommandRegistry)command).IsAsync))
            {
                // The command works asynchronously, thus we must wait until all operations initiated by the command
                // are finished, before we can go on with the next command.
                // Therefore, we acquire a unique Id to identify the async command and we associate the index of the
                // following command with this index.
                IAsyncCommand asyncCommand = (IAsyncCommand)command;

                long processSequenceId = AcquireCommand();

                // The async command is called with the NextCommand delegate:
                asyncCommand.Execute(commandParameter, delegate(bool success)
                {
                    //commandChain, processSequenceId and onlyIFinallyCommands are stored (closure).
                    AsyncExecuteRecursion(commandChain, processSequenceId, success, onlyIFinallyCommands, registryFinishedCallback);
                });
                return;
            }

            // The command works synchronously, thus we can directly proceed with the next command:
            try
            {
                command.Execute(commandParameter);
            }
            catch (Exception ex)
            {
                Log.Warn("Execution of the command '" + command.GetType().Name + "' with parameter '" + commandParameter.GetType().Name + "' lead to the following exception: " + ex);
                onlyIFinallyCommands = true;
            }
            ExecuteRecursion(commandChain, onlyIFinallyCommands, registryFinishedCallback);
        }