Пример #1
0
        public int StartExecutionLoop()
        {
            InitializeCurrentSession();

            Roundtrip roundtrip = null;
            var       stopWatch = new Stopwatch();

            try
            {
                while (true)
                {
                    _sessionViewModel.CommandPrompt = GetPrompt();

                    var queueItem = _executionQueue.Dequeue();

                    if (queueItem is LoadSessionRequest loadSessionRequest)
                    {
                        LoadSession();
                        if (loadSessionRequest.RunAfterLoad)
                        {
                            NotifyExecuteAll();
                        }
                        continue;
                    }

                    if (queueItem is CodeCompletionRequest codeCompletionRequest)
                    {
                        DoCodeCompletion(codeCompletionRequest);
                        continue;
                    }

                    var input = new PSDataCollection <PSObject>();
                    input.Complete();

                    var output = new PSDataCollection <PSObject>();

                    var request = queueItem as ExecutionRequest;
                    roundtrip = request.Roundtrip;
                    SessionViewModel.ActiveOutput = roundtrip.ViewModel;

                    var commandLine = roundtrip.ViewModel.CommandLine;
                    _powerShell.Commands.Clear();
                    _powerShell.AddScript(commandLine);
                    _powerShell.AddCommand("Out-NotebookInternal");
                    _powerShell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);

                    roundtrip.ViewModel.Hidden();
                    roundtrip.ViewModel.ClearOutput();
                    roundtrip.ViewModel.ShowExecuting();

                    try
                    {
                        ResetCancelState();
                        stopWatch.Restart();

                        var asyncResult = _powerShell.BeginInvoke(input, output);

                        WaitHandle.WaitAny(new WaitHandle[] { asyncResult.AsyncWaitHandle, _cancelEvent });

                        _stdout.Flush();

                        if (_cancelled)
                        {
                            _powerShell.Stop();
                            roundtrip.ViewModel.WriteLine("^C");
                        }

                        _powerShell.EndInvoke(asyncResult);
                    }
                    catch (RuntimeException ex)
                    {
                        DisplayError(ex);
                    }

                    stopWatch.Stop();

                    _sessionViewModel.TimeTaken = stopWatch.Elapsed;
                    _sessionViewModel.HideProgress();
                    roundtrip.ViewModel.ShowEditing();

                    if (_cancelled)
                    {
                        foreach (var item in _executionQueue.Enumerate())
                        {
                            if (item is ExecutionRequest r)
                            {
                                r.Roundtrip.ViewModel.ShowEditing();
                            }
                        }
                    }

                    if (request.MoveToNext)
                    {
                        if (_sessionViewModel.IsLastItem(roundtrip.ViewModel))
                        {
                            CreateNewRoundtrip(true);
                        }
                        else
                        {
                            var rr = _sessionViewModel.GetNextRoundtripViewModel(roundtrip.ViewModel);
                            rr.Focus();
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // pass
            }

            return(_exitCode);
        }