protected override void ProcessRecord() { DateTime utcNow = DateTime.UtcNow; base.Events.ReceivedEvents.PSEventReceived += new PSEventReceivedEventHandler(this.ReceivedEvents_PSEventReceived); bool flag = false; this.ScanEventQueue(); PSLocalEventManager events = (PSLocalEventManager)base.Events; while (!flag) { if (this.timeoutInSeconds >= 0) { TimeSpan span = (TimeSpan)(DateTime.UtcNow - utcNow); if (span.TotalSeconds > this.timeoutInSeconds) { break; } } flag = this.eventArrived.WaitOne(200, false); events.ProcessPendingActions(); } base.Events.ReceivedEvents.PSEventReceived -= new PSEventReceivedEventHandler(this.ReceivedEvents_PSEventReceived); if (this.receivedEvent != null) { base.WriteObject(this.receivedEvent); } }
internal void SubscribeEvents(ServerSteppablePipelineDriver driver) { lock (_syncObject) { if (!_initialized) { _eventManager = (object)driver.LocalPowerShell.Runspace.Events as PSLocalEventManager; if (_eventManager != null) { _startSubscriber = _eventManager.SubscribeEvent(this, "StartSteppablePipeline", Guid.NewGuid().ToString(), null, new PSEventReceivedEventHandler(this.HandleStartEvent), true, false, true); _processSubscriber = _eventManager.SubscribeEvent(this, "RunProcessRecord", Guid.NewGuid().ToString(), null, new PSEventReceivedEventHandler(this.HandleProcessRecord), true, false, true); } _initialized = true; } } }
/// <summary> /// Wait for the event to arrive. /// </summary> protected override void ProcessRecord() { DateTime startTime = DateTime.UtcNow; // Subscribe to notification of events received Events.ReceivedEvents.PSEventReceived += ReceivedEvents_PSEventReceived; bool received = false; // Scan the queue to see if it's already arrived ScanEventQueue(); // And wait for our event handler (or Control-C processor) to give us control PSLocalEventManager eventManager = (PSLocalEventManager)Events; while (!received) { if (_timeoutInSeconds >= 0) { if ((DateTime.UtcNow - startTime).TotalSeconds > _timeoutInSeconds) { break; } } received = _eventArrived.WaitOne(200); eventManager.ProcessPendingActions(); } // Unsubscribe, and write the event information we received Events.ReceivedEvents.PSEventReceived -= ReceivedEvents_PSEventReceived; if (_receivedEvent != null) { WriteObject(_receivedEvent); } }
private void InvokeHelper() { PipelineProcessor item = null; try { base.RaisePipelineStateEvents(); this.RecordPipelineStartTime(); try { item = this.CreatePipelineProcessor(); } catch (Exception exception) { if (base.SetPipelineSessionState) { base.SetHadErrors(true); this.Runspace.ExecutionContext.AppendDollarError(exception); } throw; } if (this.useExternalInput) { item.ExternalInput = base.InputStream.ObjectReader; } item.ExternalSuccessOutput = base.OutputStream.ObjectWriter; item.ExternalErrorOutput = base.ErrorStream.ObjectWriter; if (!this.IsChild) { this.LocalRunspace.ExecutionContext.InternalHost.InternalUI.SetInformationalMessageBuffers(base.InformationalBuffers); } bool questionMarkVariableValue = true; bool exceptionHandlerInEnclosingStatementBlock = this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock; this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock = false; try { this._stopper.Push(item); if (!base.AddToHistory) { questionMarkVariableValue = this.LocalRunspace.ExecutionContext.QuestionMarkVariableValue; this.LocalRunspace.ExecutionContext.IgnoreScriptDebug = true; } else { this.LocalRunspace.ExecutionContext.IgnoreScriptDebug = false; } if (!this.IsNested && !base.IsPulsePipeline) { this.LocalRunspace.ExecutionContext.ResetRedirection(); } try { item.Execute(); base.SetHadErrors(item.ExecutionFailed); } catch (ExitException exception2) { base.SetHadErrors(item.ExecutionFailed); int newValue = 1; if (this.IsNested) { try { newValue = (int)exception2.Argument; this.LocalRunspace.ExecutionContext.SetVariable(SpecialVariables.LastExitCodeVarPath, newValue); return; } finally { try { this.LocalRunspace.ExecutionContext.EngineHostInterface.ExitNestedPrompt(); } catch (ExitNestedPromptException) { } } } try { newValue = (int)exception2.Argument; } finally { this.LocalRunspace.ExecutionContext.EngineHostInterface.SetShouldExit(newValue); } } catch (ExitNestedPromptException) { } catch (FlowControlException) { } catch (Exception) { base.SetHadErrors(true); throw; } } finally { if ((item != null) && (item.Commands != null)) { for (int i = 0; i < item.Commands.Count; i++) { CommandProcessorBase base2 = item.Commands[i]; EtwActivity.SetActivityId(base2.PipelineActivityId); MshLog.LogCommandLifecycleEvent(base2.Context, CommandState.Terminated, base2.Command.MyInvocation); } } PSLocalEventManager events = this.LocalRunspace.Events as PSLocalEventManager; if (events != null) { events.ProcessPendingActions(); } this.LocalRunspace.ExecutionContext.ExceptionHandlerInEnclosingStatementBlock = exceptionHandlerInEnclosingStatementBlock; if (!this.IsChild) { this.LocalRunspace.ExecutionContext.InternalHost.InternalUI.SetInformationalMessageBuffers(null); } this._stopper.Pop(false); if (!base.AddToHistory) { this.LocalRunspace.ExecutionContext.QuestionMarkVariableValue = questionMarkVariableValue; } } } catch (FlowControlException) { } finally { if (item != null) { item.Dispose(); item = null; } } }