private void AddDataEventHandlers() { // Create new collection objects. if (_debugBlockingCollection != null) { _debugBlockingCollection.Dispose(); } if (_debugAccumulateCollection != null) { _debugAccumulateCollection.Dispose(); } _debugBlockingCollection = new PSDataCollection <PSStreamObject>(); _debugBlockingCollection.BlockingEnumerator = true; _debugAccumulateCollection = new PSDataCollection <PSStreamObject>(); _runningPowerShell = _runspace.GetCurrentBasePowerShell(); if (_runningPowerShell != null) { if (_runningPowerShell.OutputBuffer != null) { _runningPowerShell.OutputBuffer.DataAdding += HandlePowerShellOutputBufferDataAdding; } if (_runningPowerShell.ErrorBuffer != null) { _runningPowerShell.ErrorBuffer.DataAdding += HandlePowerShellErrorBufferDataAdding; } } else { _runningPipeline = _runspace.GetCurrentlyRunningPipeline(); if (_runningPipeline != null) { if (_runningPipeline.Output != null) { _runningPipeline.Output.DataReady += HandlePipelineOutputDataReady; } if (_runningPipeline.Error != null) { _runningPipeline.Error.DataReady += HandlePipelineErrorDataReady; } } } }
/// <summary> /// Protected virtual implementation of Dispose. /// </summary> /// <param name="disposing"></param> private void Dispose(bool disposing) { if (_disposed) { return; } lock (_syncLock) { if (!_disposed) { if (disposing) { _inputStream.Dispose(); _outputStream.Dispose(); _errorStream.Dispose(); _warningStream.Dispose(); _progressStream.Dispose(); _verboseStream.Dispose(); _debugStream.Dispose(); _informationStream.Dispose(); _inputStream = null; _outputStream = null; _errorStream = null; _warningStream = null; _progressStream = null; _verboseStream = null; _debugStream = null; _informationStream = null; } _disposed = true; } } }
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); }
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); }