public async Task <string> InvokeReadLineAsync(bool isCommandLine, CancellationToken cancellationToken)
        {
            _readLineCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            var localTokenSource = _readLineCancellationSource;

            if (localTokenSource.Token.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            if (!isCommandLine)
            {
                return(await _consoleReadLine.InvokeLegacyReadLineAsync(
                           isCommandLine : false,
                           _readLineCancellationSource.Token).ConfigureAwait(false));
            }

            var readLineCommand = new PSCommand()
                                  .AddCommand(s_lazyInvokeReadLineForEditorServicesCmdletInfo.Value)
                                  .AddParameter("CancellationToken", _readLineCancellationSource.Token);

            IEnumerable <string> readLineResults = await _powerShellContext.ExecuteCommandAsync <string>(
                readLineCommand,
                errorMessages : null,
                s_psrlExecutionOptions).ConfigureAwait(false);

            string line = readLineResults.FirstOrDefault();

            return(cancellationToken.IsCancellationRequested
                ? string.Empty
                : line);
        }
示例#2
0
        public async Task <string> InvokeReadLineAsync(bool isCommandLine, CancellationToken cancellationToken)
        {
            _readLineCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            var localTokenSource = _readLineCancellationSource;

            if (localTokenSource.Token.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            try
            {
                if (!isCommandLine)
                {
                    return(await _consoleReadLine.InvokeLegacyReadLineAsync(
                               false,
                               _readLineCancellationSource.Token));
                }

                var result = (await _powerShellContext.ExecuteCommandAsync <string>(
                                  new PSCommand()
                                  .AddScript(ReadLineScript)
                                  .AddArgument(_readLineCancellationSource.Token),
                                  null,
                                  new ExecutionOptions()
                {
                    WriteErrorsToHost = false,
                    WriteOutputToHost = false,
                    InterruptCommandPrompt = false,
                    AddToHistory = false,
                    IsReadLine = isCommandLine
                }))
                             .FirstOrDefault();

                return(cancellationToken.IsCancellationRequested
                    ? string.Empty
                    : result);
            }
            finally
            {
                _readLineCancellationSource = null;
            }
        }
 public Task <string> InvokeReadLineAsync(bool isCommandLine, CancellationToken cancellationToken)
 {
     return(_legacyReadLine.InvokeLegacyReadLineAsync(isCommandLine, cancellationToken));
 }