Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/NativeCommandProcessor.cs (C#)

Code before:
                        // If adding is not complete we need a try {} catch {}
                        // to mitigate a concurrent call to CompleteAdding().
                        return _nativeProcessOutputQueue.Take();
                    }
                    catch (InvalidOperationException)
                    {
                        // It's a normal situation: another thread can mark collection as CompleteAdding
                        // in a concurrent way and we will rise an exception in Take().
                        // Although it's a normal situation it's not the most common path
                        // and will be executed only on the race condtion case.
                    }
                }

                return null;
            }

            _nativeProcessOutputQueue.TryTake(out ProcessOutputObject record);
            return record;
        }

        /// <summary>
        /// Read the output from the native process and send it down the line.
        /// </summary>
        private void ConsumeAvailableNativeProcessOutput(bool blocking)
        {
            if (!_isRunningInBackground)
            {
                return;
            }

            bool stdOutRedirected = _nativeProcess.StartInfo.RedirectStandardOutput;
            bool stdErrRedirected = _nativeProcess.StartInfo.RedirectStandardError;
            if (stdOutRedirected && _stdOutByteTransfer is not null)
            {
                if (blocking)
                {
                    _stdOutByteTransfer.EOF.GetAwaiter().GetResult();
                }

                if (!stdErrRedirected)
                {
                    return;
                }
            }

            if (stdOutRedirected || stdErrRedirected)
            {
                ProcessOutputObject record;
                while ((record = DequeueProcessOutput(blocking)) != null)
                {
                    if (this.Command.Context.CurrentPipelineStopping)

Review comment (suggestion):
```suggestion
            if (_isRunningInBackground)
```

Code after:
                        // If adding is not complete we need a try {} catch {}
                        // to mitigate a concurrent call to CompleteAdding().
                        return _nativeProcessOutputQueue.Take();
                    }
                    catch (InvalidOperationException)
                    {
                        // It's a normal situation: another thread can mark collection as CompleteAdding
                        // in a concurrent way and we will rise an exception in Take().
                        // Although it's a normal situation it's not the most common path
                        // and will be executed only on the race condtion case.
                    }
                }

                return null;
            }

            _nativeProcessOutputQueue.TryTake(out ProcessOutputObject record);
            return record;
        }

        /// <summary>
        /// Read the output from the native process and send it down the line.
        /// </summary>
        private void ConsumeAvailableNativeProcessOutput(bool blocking)
        {
            if (_isRunningInBackground)
            {
                return;
            }

            bool stdOutRedirected = _nativeProcess.StartInfo.RedirectStandardOutput;
            bool stdErrRedirected = _nativeProcess.StartInfo.RedirectStandardError;
            if (stdOutRedirected && _stdOutByteTransfer is not null)
            {
                if (blocking)
                {
                    _stdOutByteTransfer.EOF.GetAwaiter().GetResult();
                }

                if (!stdErrRedirected)
                {
                    return;
                }
            }

            if (stdOutRedirected || stdErrRedirected)
            {
                ProcessOutputObject record;
                while ((record = DequeueProcessOutput(blocking)) != null)
                {
                    if (this.Command.Context.CurrentPipelineStopping)