示例#1
0
        /// <summary>
        /// Start the psuedoconsole and run the process as shown in
        /// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session#creating-the-pseudoconsole
        /// </summary>
        /// <param name="command">the command to run, e.g. cmd.exe</param>
        /// <param name="consoleHeight">The height (in characters) to start the pseudoconsole with. Defaults to 80.</param>
        /// <param name="consoleWidth">The width (in characters) to start the pseudoconsole with. Defaults to 30.</param>
        public void Start(string command, string directory, int consoleWidth = 80, int consoleHeight = 30)
        {
            _inputPipe     = new PseudoConsolePipe();
            _outputPipe    = new PseudoConsolePipe();
            _pseudoConsole = PseudoConsole.Create(_inputPipe.ReadSide, _outputPipe.WriteSide, consoleWidth, consoleHeight);

            using (var process = ProcessFactory.Start(command, directory, PseudoConsole.PseudoConsoleThreadAttribute, _pseudoConsole.Handle))
            {
                // copy all pseudoconsole output to a FileStream and expose it to the rest of the app
                ConsoleOutStream = new FileStream(_outputPipe.ReadSide, FileAccess.Read);
                OutputReady.Invoke(this, EventArgs.Empty);

                // Store input pipe handle, and a writer for later reuse
                _consoleInputPipeWriteHandle = _inputPipe.WriteSide;
                _consoleInputWriter          = new StreamWriter(new FileStream(_consoleInputPipeWriteHandle, FileAccess.Write))
                {
                    AutoFlush = true
                };

                // free resources in case the console is ungracefully closed (e.g. by the 'x' in the window titlebar)
                OnClose(() => DisposeResources(process, _pseudoConsole, _outputPipe, _inputPipe, _consoleInputWriter));

                WaitForExit(process).WaitOne(Timeout.Infinite);
            }
            Exited?.Invoke(this, EventArgs.Empty);
        }
示例#2
0
        /// <summary>
        /// Start the psuedoconsole and run the process as shown in
        /// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session#creating-the-pseudoconsole
        /// </summary>
        /// <param name="command">the command to run, e.g. cmd.exe</param>
        /// <param name="consoleHeight">The height (in characters) to start the pseudoconsole with. Defaults to 80.</param>
        /// <param name="consoleWidth">The width (in characters) to start the pseudoconsole with. Defaults to 30.</param>
        public void Start(string command, string directory, string environment, int consoleWidth = 80, int consoleHeight = 30)
        {
            _inputPipe     = new PseudoConsolePipe();
            _outputPipe    = new PseudoConsolePipe();
            _pseudoConsole = PseudoConsole.Create(_inputPipe.ReadSide, _outputPipe.WriteSide, consoleWidth, consoleHeight);

            _process = ProcessFactory.Start(command, directory, environment, PseudoConsole.PseudoConsoleThreadAttribute, _pseudoConsole.Handle);

            // copy all pseudoconsole output to a FileStream and expose it to the rest of the app
            ConsoleOutStream = new FileStream(_outputPipe.ReadSide, FileAccess.Read);
            OutputReady.Invoke(this, EventArgs.Empty);

            // Store input pipe handle, and a writer for later reuse
            _consoleInputPipeWriteHandle = _inputPipe.WriteSide;
            _consoleInputWriter          = new FileStream(_consoleInputPipeWriteHandle, FileAccess.Write);

            WaitForExit(_process).WaitOne(Timeout.Infinite);
            this.ExitCode = (int)_process.GetExitCode();

            Exited?.Invoke(this, EventArgs.Empty);
        }