/// <summary> /// Executes the first child activity /// </summary> /// <param name="executionContext">The execution context of pipeline activity.</param> protected override void Execute(NativeActivityContext executionContext) { int count = 0; if (this.Activities != null) { count = this.Activities.Count; } if (count == 0) { throw new ArgumentException(ActivityResources.NoChildPipeline); } if (this.inputValidationFailed && Input != null && Input.Expression != null && this.Activities[0].Input != null && this.Activities[0].Input.Expression != null) { throw new ArgumentException(ActivityResources.DuplicateInputDefinedInPipeline); } if (this.resultValidationFailed && Result != null && Result.Expression != null && this.Activities[count - 1].Result != null && this.Activities[count - 1].Result.Expression != null) { throw new ArgumentException(ActivityResources.DuplicateResultDefinedInPipeline); } //Executing the first child activity. PipelineEnabledActivity firstChild = this.Activities[0]; executionContext.ScheduleActivity(firstChild, new CompletionCallback(InternalExecute)); }
private void InternalExecute(NativeActivityContext executionContext, ActivityInstance completedInstance) { int completedInstanceIndex; // Reading the value of pipeline activity variables from the context. completedInstanceIndex = this.lastIndexHint.Get(executionContext); PSDataCollection <PSObject> outValue = this.GetData(executionContext, this.OutputStream); PSDataCollection <PSObject> inValue = this.GetData(executionContext, this.InputStream); // Re-checking the index of the the child activity, which has just completed its execution. if (completedInstanceIndex >= this.Activities.Count || this.Activities[completedInstanceIndex] != completedInstance.Activity) { completedInstanceIndex = this.Activities.IndexOf((PSActivity)completedInstance.Activity); } // Calculating next child activity. int nextChildIndex = completedInstanceIndex + 1; // Checking for pipeline activity completion. if (nextChildIndex == this.Activities.Count) { if (inValue != null) { inValue.Dispose(); } if (outValue != null) { outValue.Dispose(); } return; } // Setting up the environment for next child activity to run. if (outValue != null) { outValue.Complete(); } if (inValue != null) { inValue.Dispose(); } inValue = outValue; outValue = new PSDataCollection <PSObject>(); // The pipeline is complete if there is no input // PS > function foo { $input | Write-Output "Hello" } // PS > foo // PS > if ((inValue == null) || (inValue.Count == 0)) { if (outValue != null) { outValue.Dispose(); } return; } this.SetData(executionContext, this.OutputStream, outValue); this.SetData(executionContext, this.InputStream, inValue); // Executing the next child activity. PipelineEnabledActivity nextChild = this.Activities[nextChildIndex]; executionContext.ScheduleActivity(nextChild, new CompletionCallback(InternalExecute)); this.lastIndexHint.Set(executionContext, nextChildIndex); }