private static void DefaultRunspace_StateChanged(object sender, RunspaceStateEventArgs e) { CimSessionState cimSessionState = null; Runspace runspace = (Runspace)sender; RunspaceState state = e.RunspaceStateInfo.State; switch (state) { case RunspaceState.Closed: case RunspaceState.Broken: { if (CimSessionBase.cimSessions.TryRemove(runspace.InstanceId, out cimSessionState)) { object[] instanceId = new object[2]; instanceId[0] = runspace.InstanceId; instanceId[1] = e.RunspaceStateInfo.State; DebugHelper.WriteLog(string.Format(CultureInfo.CurrentUICulture, DebugHelper.runspaceStateChanged, instanceId)); cimSessionState.Dispose(); } runspace.StateChanged -= new EventHandler <RunspaceStateEventArgs>(CimSessionBase.DefaultRunspace_StateChanged); return; } case RunspaceState.Closing: { return; } default: { return; } } }
private void HandleRunspaceStateChanged(object source, RunspaceStateEventArgs stateEventArgs) { switch (stateEventArgs.RunspaceStateInfo.State) { case RunspaceState.BeforeOpen: break; case RunspaceState.Opening: break; case RunspaceState.Closing: break; default: OperationStateEventArgs e = new OperationStateEventArgs(); e.BaseEvent = (EventArgs)stateEventArgs; if (stateEventArgs.RunspaceStateInfo.State == RunspaceState.Opened) { e.OperationState = OperationState.StartComplete; OpenRunspaceOperation.tracer.WriteLine("Runspace opened for {0}", (object)this.runspace.InstanceId); } else { e.OperationState = OperationState.StopComplete; OpenRunspaceOperation.tracer.WriteLine("Runspace closed for {0}", (object)this.runspace.InstanceId); } this.OperationComplete((object)this, e); break; } }
private void OnRunspaceStateChanged(object sender, RunspaceStateEventArgs e) { if (RunspaceState.Closing == e.RunspaceStateInfo.State) { _runspace.StateChanged -= OnRunspaceStateChanged; Stop(true); } }
private void HandleRunspaceStateChanged(object sender, RunspaceStateEventArgs e) { if (!(sender is RemoteRunspace remoteRunspace) || e.RunspaceStateInfo.State == RunspaceState.Opening) { return; } remoteRunspace.URIRedirectionReported -= new EventHandler <RemoteDataEventArgs <Uri> >(((PSRemotingChildJob)this).HandleURIDirectionReported); remoteRunspace.StateChanged -= new EventHandler <RunspaceStateEventArgs>(this.HandleRunspaceStateChanged); }
private void HandleRunspaceStateChanged(object sender, RunspaceStateEventArgs e) { RemoteRunspace runspace = sender as RemoteRunspace; if ((runspace != null) && (e.RunspaceStateInfo.State != RunspaceState.Opening)) { runspace.URIRedirectionReported -= new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported); runspace.StateChanged -= new EventHandler <RunspaceStateEventArgs>(this.HandleRunspaceStateChanged); } }
private void OnRunspaceStateChanged(object sender, RunspaceStateEventArgs e) { lock (this.sessionStateLock) { if (e.RunspaceStateInfo.State == RunspaceState.Broken) { PowwaEvents.PowwaEVENT_PSREXECUTION_FAILURE(this.Name, e.RunspaceStateInfo.Reason.Message); this.PostClientMessage(new SessionTerminatedMessage(), false); } } }
private void CheckIfRunspaceIsOpening(object sender, RunspaceStateEventArgs runspaceStateEventArgs) { if (runspaceStateEventArgs.RunspaceStateInfo.State != RunspaceState.Opening) { _opened.Set(); } else { _opened.Reset(); } }
private void Runspace_StateChanged(object sender, RunspaceStateEventArgs e) { ServiceCommon.Log("Remote runspace State Changed: {0}", e.RunspaceStateInfo.State); switch (e.RunspaceStateInfo.State) { case RunspaceState.Broken: case RunspaceState.Closed: case RunspaceState.Disconnected: PopRunspace(); break; } }
private void HandleRunspaceStateChanged( object source, RunspaceStateEventArgs stateEventArgs) { switch (stateEventArgs.RunspaceStateInfo.State) { case RunspaceState.Opened: case RunspaceState.Closed: case RunspaceState.Broken: _runspace.StateChanged -= HandleRunspaceStateChanged; ReleaseWait(); break; } }
/// <summary> /// Runspace state change event handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _runspace_StateChanged(object sender, RunspaceStateEventArgs e) { ServiceCommon.Log("Runspace State Changed: {0}", e.RunspaceStateInfo.State); switch (e.RunspaceStateInfo.State) { case RunspaceState.Broken: case RunspaceState.Closed: case RunspaceState.Disconnected: if (_callback != null) { _callback.DebuggerFinished(); } break; } }
private void StateCallBackHandler(object sender, RunspaceStateEventArgs eArgs) { if (eArgs.RunspaceStateInfo.State != RunspaceState.Disconnecting) { if (eArgs.RunspaceStateInfo.State == RunspaceState.Disconnected) { this.WriteDisconnectedPSSession(); } else { this.WriteDisconnectFailed(null); } this.remoteSession.Runspace.StateChanged -= new EventHandler <RunspaceStateEventArgs>(this.StateCallBackHandler); this.SendStartComplete(); } }
void _runspace_StateChanged(object sender, RunspaceStateEventArgs e) { Log.InfoFormat("Runspace State Changed: {0}", e.RunspaceStateInfo.State); switch (e.RunspaceStateInfo.State) { case RunspaceState.Broken: case RunspaceState.Closed: case RunspaceState.Disconnected: if (DebuggingFinished != null) { DebuggingFinished(this, new EventArgs()); } break; } }
private void StateCallBackHandler(object sender, RunspaceStateEventArgs eArgs) { if (eArgs.RunspaceStateInfo.State == RunspaceState.Connecting || eArgs.RunspaceStateInfo.State == RunspaceState.Disconnecting || eArgs.RunspaceStateInfo.State == RunspaceState.Disconnected) { return; } Dbg.Assert(eArgs.RunspaceStateInfo.State != RunspaceState.BeforeOpen, "Can't reconnect a session that hasn't been previously Opened"); Dbg.Assert(eArgs.RunspaceStateInfo.State != RunspaceState.Opening, "Can't reconnect a session that hasn't been previously Opened"); if (eArgs.RunspaceStateInfo.State == RunspaceState.Opened) { // Connect operation succeeded, write the PSSession object. WriteConnectedPSSession(); } else { // Check to see if failure is due to stale PSSession error and // add to retry list if this is the case. bool writeError = true; if (_queryRunspaces == null) { PSRemotingTransportException transportException = eArgs.RunspaceStateInfo.Reason as PSRemotingTransportException; if (transportException != null && transportException.ErrorCode == WSManNativeApi.ERROR_WSMAN_INUSE_CANNOT_RECONNECT) { lock (s_LockObject) { _retryList.Add(_session); } writeError = false; } } if (writeError) { // Connect operation failed, write error. WriteConnectFailed(eArgs.RunspaceStateInfo.Reason, _session); } } _session.Runspace.StateChanged -= StateCallBackHandler; SendStartComplete(); }
private static void CurrentRunspace_StateChanged(object sender, RunspaceStateEventArgs e) { switch (e.RunspaceStateInfo.State) { case RunspaceState.Broken: case RunspaceState.Closed: try { GetRandomCommand.s_runspaceGeneratorMapLock.EnterWriteLock(); GetRandomCommand.s_runspaceGeneratorMap.Remove(((Runspace)sender).InstanceId); } finally { GetRandomCommand.s_runspaceGeneratorMapLock.ExitWriteLock(); } break; } }
private void HandleRunspaceStateChanged(object sender, RunspaceStateEventArgs stateEventArgs) { switch (stateEventArgs.RunspaceStateInfo.State) { case RunspaceState.Opened: try { if (this.invokeAndDisconnect) { base.pipeline.InvokeAsyncAndDisconnect(); } else { base.pipeline.InvokeAsync(); } } catch (InvalidPipelineStateException) { this.remoteRunspace.CloseAsync(); } catch (InvalidRunspaceStateException exception) { base.internalException = exception; this.remoteRunspace.CloseAsync(); } return; case RunspaceState.Closed: if (stateEventArgs.RunspaceStateInfo.Reason == null) { this.RaiseOperationCompleteEvent(); return; } this.RaiseOperationCompleteEvent(stateEventArgs); return; case RunspaceState.Broken: this.RaiseOperationCompleteEvent(stateEventArgs); return; } }
private void OnRunspaceStateEvent(object sender, RunspaceStateEventArgs e) { //! Carefully process events other than 'Opened'. if (e.RunspaceStateInfo.State != RunspaceState.Opened) { // alive? do nothing, wait for other events if (e.RunspaceStateInfo.State != RunspaceState.Broken) { return; } // broken; keep an error silently //errorFatal = e.RunspaceStateInfo.Reason; //! Set the broken flag, waiting threads may continue. //! The last code, Invoking() may be waiting for this. isRunspaceOpenedOrBroken = true; return; } Engine = host.Runspace.SessionStateProxy.PSVariable.GetValue("ExecutionContext") as EngineIntrinsics; }
/// <summary> /// <para> /// clean up the dictionaries if the runspace is closed or broken. /// </para> /// </summary> /// <param name="sender">Runspace</param> /// <param name="e">Event args</param> private static void DefaultRunspace_StateChanged(object sender, RunspaceStateEventArgs e) { Runspace runspace = (Runspace)sender; switch (e.RunspaceStateInfo.State) { case RunspaceState.Broken: case RunspaceState.Closed: CimSessionState state; if (cimSessions.TryRemove(runspace.InstanceId, out state)) { DebugHelper.WriteLog(String.Format(CultureInfo.CurrentUICulture, DebugHelper.runspaceStateChanged, runspace.InstanceId, e.RunspaceStateInfo.State)); state.Dispose(); } runspace.StateChanged -= DefaultRunspace_StateChanged; break; default: break; } }
private void StateCallBackHandler(object sender, RunspaceStateEventArgs eArgs) { if (eArgs.RunspaceStateInfo.State == RunspaceState.Disconnecting) { return; } if (eArgs.RunspaceStateInfo.State == RunspaceState.Disconnected) { // If disconnect succeeded then write the PSSession object. WriteDisconnectedPSSession(); } else { // Write error if disconnect did not succeed. WriteDisconnectFailed(); } // Notify throttle manager that the start is complete. _remoteSession.Runspace.StateChanged -= StateCallBackHandler; SendStartComplete(); }
private void StateCallBackHandler(object sender, RunspaceStateEventArgs eArgs) { if (eArgs.RunspaceStateInfo.State == RunspaceState.Connecting || eArgs.RunspaceStateInfo.State == RunspaceState.Disconnecting || eArgs.RunspaceStateInfo.State == RunspaceState.Disconnected) { return; } else { if (eArgs.RunspaceStateInfo.State != RunspaceState.Opened) { bool flag = true; if (this._queryRunspaces == null) { PSRemotingTransportException reason = eArgs.RunspaceStateInfo.Reason as PSRemotingTransportException; if (reason != null && reason.ErrorCode == -2144108083) { lock (ConnectPSSessionCommand.ConnectRunspaceOperation.s_LockObject) { this._retryList.Add(this._session); } flag = false; } } if (flag) { this.WriteConnectFailed(eArgs.RunspaceStateInfo.Reason, this._session); } } else { this.WriteConnectedPSSession(); } this._session.Runspace.StateChanged -= new EventHandler <RunspaceStateEventArgs>(this.StateCallBackHandler); this.SendStartComplete(); return; } }
private void HandleRunspaceStateChanged(object source, RunspaceStateEventArgs stateEventArgs) { switch (stateEventArgs.RunspaceStateInfo.State) { case RunspaceState.BeforeOpen: case RunspaceState.Opening: case RunspaceState.Closing: return; } OperationStateEventArgs operationStateEventArgs = null; lock (this._syncObject) { if (!this.stopComplete) { this.stopComplete = true; this.startComplete = true; operationStateEventArgs = new OperationStateEventArgs { BaseEvent = stateEventArgs, OperationState = OperationState.StopComplete }; } else if (!this.startComplete) { this.startComplete = true; operationStateEventArgs = new OperationStateEventArgs { BaseEvent = stateEventArgs, OperationState = OperationState.StartComplete }; } } if (operationStateEventArgs != null) { this.FireEvent(operationStateEventArgs); } }
private void HandleRunspaceStateChanged(object sender, RunspaceStateEventArgs stateEventArgs) { switch (stateEventArgs.RunspaceStateInfo.State) { case RunspaceState.Opened: try { this.pipeline.InvokeAsync(); break; } catch (InvalidPipelineStateException ex) { this.remoteRunspace.CloseAsync(); break; } catch (InvalidRunspaceStateException ex) { this.internalException = (Exception)ex; this.remoteRunspace.CloseAsync(); break; } case RunspaceState.Closed: if (stateEventArgs.RunspaceStateInfo.Reason != null) { this.RaiseOperationCompleteEvent((EventArgs)stateEventArgs); break; } this.RaiseOperationCompleteEvent(); break; case RunspaceState.Broken: this.RaiseOperationCompleteEvent((EventArgs)stateEventArgs); break; } }
private static void CurrentRunspace_StateChanged(object sender, RunspaceStateEventArgs e) { switch (e.RunspaceStateInfo.State) { case RunspaceState.Closed: case RunspaceState.Broken: try { runspaceGeneratorMapLock.AcquireWriterLock(-1); RunspaceGeneratorMap.Remove(((Runspace)sender).InstanceId); } finally { runspaceGeneratorMapLock.ReleaseWriterLock(); } break; case RunspaceState.Closing: break; default: return; } }
private void RunspaceStateChanged(object sender, RunspaceStateEventArgs e) { Guid guid = this._instanceId; this._structuredTracer.RunspaceStateChanged(guid.ToString(), e.RunspaceStateInfo.State.ToString(), string.Empty); this._tracer.WriteMessage(string.Concat("PSW Conn: runspace state", e.RunspaceStateInfo.State.ToString())); RunspaceState state = e.RunspaceStateInfo.State; switch (state) { case RunspaceState.Opened: { this.RetryAttempt = 0; this.ReadyForReconnect = false; this.AsyncResult.Connection = this; this.AsyncResult.SetAsCompleted(null); this.RaiseEvents(1); this.RaiseEvents(4); return; } case RunspaceState.Closed: { this.DisposeRunspace(); this.RaiseEvents(2); return; } case RunspaceState.Broken: { if (this.RetryCount <= 0 || this.RetryAttempt >= this.RetryCount) { this.Busy = false; lock (this._syncObject) { if (this.AsyncResult != null) { this.AsyncResult.Connection = null; this.AsyncResult.SetAsCompleted(e.RunspaceStateInfo.Reason); } } object[] computerName = new object[1]; computerName[0] = this._runspace.ConnectionInfo.ComputerName; this._tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "Disposing broken connection to {0}", computerName)); this.DisposeRunspace(); this.RaiseEvents(1); this.RaiseEvents(3); this.RaiseEvents(2); this.RaiseEvents(4); return; } else { Connection retryAttempt = this; retryAttempt.RetryAttempt = retryAttempt.RetryAttempt + 1; Timer timer = new Timer(); timer.AutoReset = false; timer.Enabled = false; timer.Interval = (double)((float)(this._retryInterval * 0x3e8)); Timer timer1 = timer; timer1.Elapsed += new ElapsedEventHandler(this.RetryTimerElapsed); timer1.Start(); return; } } case RunspaceState.Disconnecting: { this.ReadyForDisconnect = false; return; } case RunspaceState.Disconnected: { this.ReadyForReconnect = true; this.RaiseEvents(3); return; } default: { return; } } }
//!STOP! // With 'Opened' state it is called from another thread. // Also, it can be broken, e.g. x86 build may fail on x64 machine. void OnRunspaceStateEvent(object sender, RunspaceStateEventArgs e) { //! Carefully process events other than 'Opened'. if (e.RunspaceStateInfo.State != RunspaceState.Opened) { // alive? do nothing, wait for other events if (e.RunspaceStateInfo.State != RunspaceState.Broken) { return; } // broken; keep an error silently _errorFatal = e.RunspaceStateInfo.Reason; //! Set the broken flag, waiting threads may continue. //! The last code, Invoking() may be waiting for this. _isRunspaceOpenedOrBroken = true; return; } // Add the module path. // STOP: [_100127_182335 test] // *) Add before the profile, so that it can load modules. // *) Add after the core loading so that standard paths are added. // *) Check for already added, e.g. when starting from another Far. var modulePathAdd = string.Concat(AppHome, "\\Modules;"); var modulePathNow = Environment.GetEnvironmentVariable(Word.PSModulePath); if (!modulePathNow.Contains(modulePathAdd)) { Environment.SetEnvironmentVariable(Word.PSModulePath, modulePathAdd + modulePathNow); } //! If it is async then PS catches all and adds errors to $Error. //! Thus, we don't catch anything, because this is normally async. string message = null; try { //_090315_091325 // Get engine once to avoid this: "A pipeline is already executing. Concurrent SessionStateProxy method call is not allowed." // Looks like a hack, but it works fine. Problem case: run Test-CallStack-.ps1, Esc -> the error above. // SVN tag 4.2.26 _engine_ = Runspace.SessionStateProxy.PSVariable.GetValue(Word.ExecutionContext) as EngineIntrinsics; // get version try { _PSVersion = (Version)((IDictionary)Runspace.SessionStateProxy.PSVariable.GetValue("PSVersionTable"))["PSVersion"]; } catch { throw new InvalidOperationException("Cannot get PowerShell version."); } // new variables PSVariable var1 = new PSVariable("Psf", this, ScopedItemOptions.AllScope | ScopedItemOptions.Constant); var1.Description = "Exposes PowerShellFar."; Engine.SessionState.PSVariable.Set(var1); PSVariable var2 = new PSVariable("Far", Far.Api, ScopedItemOptions.AllScope | ScopedItemOptions.Constant); var2.Description = "Exposes FarNet."; Engine.SessionState.PSVariable.Set(var2); // invoke profiles using (var ps = NewPowerShell()) { // internal profile (NB: there is trap in there) ps.AddCommand(Path.Combine(A.Psf.AppHome, "PowerShellFar.ps1"), false).Invoke(); // user profile, separately for better diagnostics var profile = Entry.RoamingData + "\\Profile.ps1"; if (File.Exists(profile)) { ps.Commands.Clear(); try { ps.AddCommand(profile, false).Invoke(); } catch (RuntimeException ex) { message = string.Format(null, @" Error in the profile: {0} Error message: {1} See $Error for details. ", profile, ex.Message); } } } } finally { // GUI message if (message != null) { Far.Api.Message(message, Res.Me, MessageOptions.Warning | MessageOptions.Gui | MessageOptions.Ok); } //! The last code, Invoking() may be waiting for this. _isRunspaceOpenedOrBroken = true; } }
void Runspace_StateChanged(object sender, RunspaceStateEventArgs e) { Tracer.TraceInformation("runspace-state-changed-to {0}", e.RunspaceStateInfo.State); }
private void HandleRunspaceStateChanged(object sender, OperationStateEventArgs stateEventArgs) { ErrorRecord errorRecord; PSRemotingTransportException exception2; string str; if (sender == null) { throw PSTraceSource.NewArgumentNullException("sender"); } if (stateEventArgs == null) { throw PSTraceSource.NewArgumentNullException("stateEventArgs"); } RunspaceStateEventArgs baseEvent = stateEventArgs.BaseEvent as RunspaceStateEventArgs; RunspaceState state = baseEvent.RunspaceStateInfo.State; OpenRunspaceOperation operation = sender as OpenRunspaceOperation; RemoteRunspace operatedRunspace = operation.OperatedRunspace; if (operatedRunspace != null) { operatedRunspace.URIRedirectionReported -= new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported); } PipelineWriter objectWriter = this.stream.ObjectWriter; Exception reason = baseEvent.RunspaceStateInfo.Reason; switch (state) { case RunspaceState.Opened: { PSSession remoteRunspaceInfo = new PSSession(operatedRunspace); base.RunspaceRepository.Add(remoteRunspaceInfo); Action <Cmdlet> action = cmdlet => cmdlet.WriteObject(remoteRunspaceInfo); if (objectWriter.IsOpen) { objectWriter.Write(action); } return; } case RunspaceState.Closed: { Uri uri = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo <Uri>(operatedRunspace.ConnectionInfo, "ConnectionUri", null); string message = base.GetMessage(RemotingErrorIdStrings.RemoteRunspaceClosed, new object[] { (uri != null) ? uri.AbsoluteUri : string.Empty }); Action <Cmdlet> action3 = cmdlet => cmdlet.WriteVerbose(message); if (objectWriter.IsOpen) { objectWriter.Write(action3); } if (reason != null) { ErrorRecord errorRecord2 = new ErrorRecord(reason, "PSSessionStateClosed", ErrorCategory.OpenError, operatedRunspace); Action <Cmdlet> action4 = cmdlet => cmdlet.WriteError(errorRecord2); if (objectWriter.IsOpen) { objectWriter.Write(action4); } } return; } case RunspaceState.Closing: return; case RunspaceState.Broken: exception2 = reason as PSRemotingTransportException; str = null; if (exception2 != null) { OpenRunspaceOperation operation2 = sender as OpenRunspaceOperation; if (operation2 != null) { string computerName = operation2.OperatedRunspace.ConnectionInfo.ComputerName; if (exception2.ErrorCode != -2144108135) { str = "[" + computerName + "] "; if (!string.IsNullOrEmpty(exception2.Message)) { str = str + exception2.Message; } else if (!string.IsNullOrEmpty(exception2.TransportMessage)) { str = str + exception2.TransportMessage; } break; } string str3 = PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.URIRedirectionReported, new object[] { exception2.Message, "MaximumConnectionRedirectionCount", "PSSessionOption", "AllowRedirection" }); str = "[" + computerName + "] " + str3; } } break; default: return; } PSRemotingDataStructureException exception3 = reason as PSRemotingDataStructureException; if (exception3 != null) { OpenRunspaceOperation operation3 = sender as OpenRunspaceOperation; if (operation3 != null) { string str4 = operation3.OperatedRunspace.ConnectionInfo.ComputerName; str = "[" + str4 + "] " + exception3.Message; } } if (reason == null) { reason = new RuntimeException(base.GetMessage(RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState, new object[] { state })); } string fQEIDFromTransportError = WSManTransportManagerUtils.GetFQEIDFromTransportError((exception2 != null) ? exception2.ErrorCode : 0, this._defaultFQEID); errorRecord = new ErrorRecord(reason, operatedRunspace, fQEIDFromTransportError, ErrorCategory.OpenError, null, null, null, null, null, str, null); Action <Cmdlet> action2 = cmdlet => cmdlet.WriteError(errorRecord); if (objectWriter.IsOpen) { objectWriter.Write(action2); } this.toDispose.Add(operatedRunspace); }
static void runspace_StateChanged(object sender, RunspaceStateEventArgs e) { // Console.WriteLine("RunspaceState = " + e.RunspaceStateInfo.ToString()); // Console.WriteLine("e.RunspaceStateInfo.Reason.Message = " + e.RunspaceStateInfo.Reason.Message); // Console.WriteLine("e.RunspaceStateInfo.State = " + e.RunspaceStateInfo.State.ToString()); }
private static void Runspace_StateChanged(object sender, RunspaceStateEventArgs e) { System.Diagnostics.Trace.WriteLine($"Sender: {sender}"); System.Diagnostics.Trace.WriteLine($"State: {e.RunspaceStateInfo.State}"); }
/// <summary> /// Triggered when runspace state change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Runspace_StateChanged(object sender, RunspaceStateEventArgs e) { InvokeRunspaceStateChangedReceived(new RunspaceStateEventArg(e.RunspaceStateInfo.State)); }
protected virtual void HandleRunspaceStateChanged(object sender, RunspaceStateEventArgs e) { }