/// <summary> /// /// Loops over the Host's sole Runspace; opens the runspace, initializes it, then recycles it if the Runspace fails. /// /// </summary> /// <returns> /// /// The process exit code to be returned by Main. /// /// </returns> private uint DoRunspaceLoop(string initialCommand, bool skipProfiles, Collection<CommandParameter> initialCommandArgs, bool staMode, bool importSystemModules, string configurationName) { ExitCode = ExitCodeSuccess; while (!ShouldEndSession) { RunspaceCreationEventArgs args = new RunspaceCreationEventArgs(initialCommand, skipProfiles, staMode, importSystemModules, configurationName, initialCommandArgs); CreateRunspace(args); if (ExitCode == ExitCodeInitFailure) { break; } if (!_noExit) { // Wait for runspace to open, init, and run init script before // setting ShouldEndSession, to allow debugger to work. ShouldEndSession = true; } else { // Start nested prompt loop. EnterNestedPrompt(); } if (_setShouldExitCalled) { ExitCode = unchecked((uint)_exitCodeFromRunspace); } else { Executor exec = new Executor(this, false, false); bool dollarHook = exec.ExecuteCommandAndGetResultAsBool("$global:?") ?? false; if (dollarHook && (_lastRunspaceInitializationException == null)) { ExitCode = ExitCodeSuccess; } else { ExitCode = ExitCodeSuccess | 0x1; } } _runspaceRef.Runspace.Close(); _runspaceRef = null; if (staMode) // don't recycle the Runspace in STA mode { ShouldEndSession = true; } } return ExitCode; }
private int DoRunspaceLoop(string initialCommand, bool skipProfiles, Collection<CommandParameter> initialCommandArgs, bool staMode, bool importSystemModules, bool showInitialPrompt) { bool valueOrDefault; this.ExitCode = 0; while (!this.ShouldEndSession) { this.promptDisplayedInNativeCode = showInitialPrompt; this.runspaceOpenedWaitHandle = new ManualResetEvent(false); RunspaceCreationEventArgs runspaceCreationEventArg = new RunspaceCreationEventArgs(initialCommand, skipProfiles, staMode, importSystemModules, initialCommandArgs); ThreadPool.QueueUserWorkItem(new WaitCallback(this.CreateRunspace), runspaceCreationEventArg); if (this.noExit || this.ui.ReadFromStdin) { this.EnterNestedPrompt(); } else { this.ShouldEndSession = true; } this.runspaceOpenedWaitHandle.WaitOne(); if (this.ExitCode == -65536) { break; } if (!this.setShouldExitCalled) { Executor executor = new Executor(this, false, false); bool? nullable = executor.ExecuteCommandAndGetResultAsBool("$global:?"); if (nullable.HasValue) { valueOrDefault = nullable.GetValueOrDefault(); } else { valueOrDefault = false; } bool flag = valueOrDefault; if (!flag) { this.ExitCode = 1; } else { this.ExitCode = 0; } } else { this.ExitCode = this.exitCodeFromRunspace; } this.runspaceRef.Runspace.Close(); this.runspaceRef = null; if (!staMode) { continue; } this.ShouldEndSession = true; } return this.ExitCode; }