/// <summary> /// Starts the given command execution as a sub-execution of this command execution. /// </summary> /// <param name="subExecution">The sub-execution to start.</param> protected void StartSubExecution(CmdExecutionBase subExecution) { if (subExecution == null) { throw new ArgumentNullException("subExecution"); } if (this.subExecution.Read() != null) { throw new InvalidOperationException("This command execution is already running a sub-execution!"); } if (subExecution.isInitialized.Read() != 0x00) { throw new ArgumentException("The given command execution has already been initialized!", "subExecution"); } if (!subExecution.RecipientEntities.SetEquals(this.RecipientEntities)) { throw new ArgumentException("The given command execution has different recipient entities than this one!", "subExecution"); } subExecution.scenario.Write(null); subExecution.owner.Write(null); subExecution.recipientEntities = null; subExecution.subExecution.Write(null); subExecution.parentExecution.Write(this); this.subExecution.Write(subExecution); /// Initialize the sub execution. this.subExecution.Read().Initialize(); this.subExecution.Read().isInitialized.Write(0x01); }
/// <summary> /// The internal implementation of CmdExecutionBase.Continue. /// </summary> /// <returns>True if this command execution has finished; otherwise false.</returns> private bool ContinueInternal() { if (this.isInitialized.Read() == 0x00) { throw new InvalidOperationException("Command execution is not initialized!"); } /// If this is not a sub-execution -> Remove the detached recipient entities from the list. if (this.parentExecution.Read() == null) { foreach (Entity recipientEntity in new List <Entity>(this.recipientEntities)) { if (recipientEntity.MapObject == null) { this.recipientEntities.Remove(recipientEntity); } } } /// If there are no more recipient entities -> execution finished. if (this.RecipientEntities.Count == 0) { return(true); } /// Check if we have a sub-execution currently in progress. if (this.subExecution.Read() != null) { /// We have a sub-execution currently in progress -> continue the sub execution. if (this.subExecution.Read().ContinueInternal()) { /// Sub-execution finished -> Dispose the sub-execution. this.subExecution.Read().Dispose(); this.subExecution.Write(null); } return(false); } /// No sub-execution currently in progress -> continue the current execution. if (this.ContinueImpl()) { if (this.parentExecution.Read() == null) { /// Current execution finished and this is not a sub-execution -> ask for continuation. CmdExecutionBase continuation = this.GetContinuation(); if (continuation != null) { /// Attach the continuation to the scenario. continuation.AttachToScenario(); } } /// Current execution finished. return(true); } /// Not finished yet. return(false); }