ConEmu.WinForms.ConEmuControl.Start C# (CSharp) Méthode

Start() private méthode

private Start ( [ startinfo ) : ConEmuSession
startinfo [
Résultat ConEmuSession
        public ConEmuSession Start([NotNull] ConEmuStartInfo startinfo)
        {
            if(startinfo == null)
                throw new ArgumentNullException(nameof(startinfo));

            // Close prev session if there is one
            _running?.CloseConsoleEmulator();
            if(_running != null)
                throw new InvalidOperationException("Cannot start a new console process because another console emulator session has failed to close in due time.");

            _autostartinfo = null; // As we're starting, no more chance for an autostart
            if(!IsHandleCreated)
                CreateHandle();

            // Spawn session
            var session = new ConEmuSession(startinfo, new ConEmuSession.HostContext((void*)Handle, IsStatusbarVisible));
            _running = session;
            StateChanged?.Invoke(this, EventArgs.Empty);

            // Wait for its exit
            session.WaitForConsoleEmulatorCloseAsync().ContinueWith(scheduler : TaskScheduler.FromCurrentSynchronizationContext(), continuationAction : task =>
            {
                try
                {
                    _nLastExitCode = _running.GetConsoleProcessExitCode();
                }
                catch(Exception)
                {
                    // NOP
                }
                _running = null;
                Invalidate();
                StateChanged?.Invoke(this, EventArgs.Empty);
            });

            return session;
        }

Usage Example

Exemple #1
0
        private static Form CreatePingForm()
        {
            var form = new Form() {AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink, Padding = new Padding(10), Text = "Ping Command"};

            FlowLayoutPanel stack;
            form.Controls.Add(stack = new FlowLayoutPanel() {Dock = DockStyle.Fill, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink, FlowDirection = FlowDirection.TopDown});

            stack.Controls.Add(new Label() {AutoSize = true, Dock = DockStyle.Top, Text = "Running the ping command.", Padding = new Padding(5)});
            Label labelWaitOrResult;
            stack.Controls.Add(labelWaitOrResult = new Label() {AutoSize = true, Dock = DockStyle.Top, Text = "Please wait…", BackColor = Color.Yellow, Padding = new Padding(5)});

            ConEmuControl conemu;
            var sbText = new StringBuilder();
            stack.Controls.Add(conemu = new ConEmuControl() {AutoStartInfo = null, MinimumSize = new Size(800, 600), Dock = DockStyle.Top});
            ConEmuSession session = conemu.Start(new ConEmuStartInfo() {AnsiStreamChunkReceivedEventSink = (sender, args) => sbText.Append(args.GetMbcsText()), ConsoleProcessCommandLine = "ping 8.8.8.8"});
            session.ConsoleProcessExited += delegate
            {
                Match match = Regex.Match(sbText.ToString(), @"\(.*\b(?<pc>\d+)%.*?\)", RegexOptions.Multiline);
                if(!match.Success)
                {
                    labelWaitOrResult.Text = "Ping execution completed, failed to parse the result.";
                    labelWaitOrResult.BackColor = Color.PaleVioletRed;
                }
                else
                {
                    labelWaitOrResult.Text = $"Ping execution completed, lost {match.Groups["pc"].Value} per cent of packets.";
                    labelWaitOrResult.BackColor = Color.Lime;
                }
            };
            session.ConsoleEmulatorClosed += delegate { form.Close(); };

            return form;
        }
All Usage Examples Of ConEmu.WinForms.ConEmuControl::Start