title: Run Process in Current Console
body: <p>I'm writing a basic shell for Windows, and I was wondering if there is any way to run a subprocess (<code>Process process</code>) so that it uses the current console window. By this I mean that I do <em>not</em> want to redirect input/output; I want the process to take input from the current console and print output directly to the same console window.</p>
  
  <p>The reason is that I want to allow this subprocess to set console colors for output, which cannot happen if I redirect the process' standard output. Also, I currently use the code</p>
  
  <pre><code>while (!process.HasExited)
      process.StandardInput.WriteLine(Console.ReadLine());
  </code></pre>
  
  <p>to redirect standard input to the process. However, if the process exits immediately after an input (for example, I type "exit" + ENTER, and the process exits), this loop will run once more, so the console is waiting for input from the user that will never be used by the process (it is about to exit).</p>
  
  <p>So, long question short, <strong>how do I run a process in the current console, so that it can set console colors and directly take input from the console</strong>?</p>
  
  <p><strong>Edit:</strong> Below are the methods that are relevant to this question from my code:</p>
  
  <pre><code>static int runExe(string exePath, params string[] args)
  {
      ProcessStartInfo startInfo = new ProcessStartInfo(exePath, args)
      {
          ErrorDialog = false,
          UseShellExecute = false,
          CreateNoWindow = true,
          RedirectStandardInput = true,
          RedirectStandardOutput = true,
          RedirectStandardError = true,
          RedirectStandardInput = true
      };
      Process process = new Process() { StartInfo = startInfo };
      process.Start();
      ReadThreadState stdout = readThread(process.StandardOutput, false);
      ReadThreadState stderr = readThread(process.StandardError, true);
      while (!process.HasExited)
          process.StandardInput.WriteLine(Console.ReadLine());
      stdout.stop = stderr.stop = true;
      return process.ExitCode;
  }
  class ReadThreadState
  {
      public bool stop;
  }
  private static ReadThreadState readThread(StreamReader reader, bool isError)
  {
      ReadThreadState state = new ReadThreadState();
      new Thread(() =&gt;
      {
          while (!state.stop)
          {
              int current;
              while ((current = reader.Read()) &gt;= 0)
                  if (isError)
                      writeError(((char)current).ToString(), ConsoleColor.Red);
                  else
                      Console.Write((char)current);
          }
      }).Start();
      return state;
  }
  </code></pre>