public void Dispose()
 {
     if (process != null)
     {
         try { Process p = Process.GetProcessById(process.ProcessInfo.dwProcessId); if (p.HasExited == false)
               {
                   p.Kill();
               }
         } catch (Exception) { }
         try { process.Dispose(); } catch (Exception) { }
         process = null;
     }
     inputPipe     = null;
     outputPipe    = null;
     pseudoConsole = null;
     writer        = null;
     mainThread    = null;
     parent        = null;
 }
        private void MainTerminalLoop()
        {
            string cmd = "cmd.exe";

            if (protocol == 9)
            {
                cmd = "powershell.exe";
            }
            using (var inputPipe = new ConPTY.PseudoConsolePipe())
                using (var outputPipe = new ConPTY.PseudoConsolePipe())
                    using (var pseudoConsole = ConPTY.PseudoConsole.Create(inputPipe.ReadSide, outputPipe.WriteSide, (short)width, (short)height))
                        using (var process = ConPTY.ProcessFactory.Start(cmd, ConPTY.PseudoConsole.PseudoConsoleThreadAttribute, pseudoConsole.Handle))
                        {
                            this.inputPipe     = inputPipe;
                            this.outputPipe    = outputPipe;
                            this.pseudoConsole = pseudoConsole;
                            this.process       = process;
                            Task.Run(() => CopyPipeToOutput(outputPipe.ReadSide, parent));                              // Output
                            writer           = new StreamWriter(new FileStream(inputPipe.WriteSide, FileAccess.Write)); // Input
                            writer.AutoFlush = true;
                            OnClose(() => DisposeResources(process, pseudoConsole, outputPipe, inputPipe));             // Close
                            WaitForExit(process).WaitOne(Timeout.Infinite);
                        }
        }
 /// <summary>
 /// Get an AutoResetEvent that signals when the process exits
 /// </summary>
 private static AutoResetEvent WaitForExit(ConPTY.XProcess process) => new AutoResetEvent(false)
 {
     SafeWaitHandle = new SafeWaitHandle(process.ProcessInfo.hProcess, ownsHandle: false)
 };