title: Sending message from one C# console application to another
body: <p>First of all, I've read all related topics and they gave general idea but implementation doesn't work for me:<br/>
  <a href="https://stackoverflow.com/questions/12843632/send-strings-from-one-console-application-to-another">Send strings from one console application to another</a> <br/>
  <a href="https://stackoverflow.com/questions/451228/how-to-send-input-to-the-console-as-if-the-user-is-typing">How to send input to the console as if the user is typing?</a> <br/>
  <a href="https://stackoverflow.com/questions/1765493/sending-input-getting-output-from-a-console-application-c-winforms">Sending input/getting output from a console application (C#/WinForms)</a> <br/>
  I have a console application that is doing some actions in background until cancellation is requested. Typical usage scenario is :<br/>
  1) Execute application <br/>
  2) Enter input data<br/>
  3) Issue start command<br/>
  4) After some time passes, enter stop command<br/>
  5) Exit application<br/>
  Child application <code>Program.cs</code> :<br/></p>
  
  <pre><code>static void Main()
  {
      Console.WriteLine("Enter input parameter : ");
      var inputParameter = Console.ReadLine();
      Console.WriteLine("Entered : " + inputParameter);
  
      var tokenSource = new CancellationTokenSource();
      var token = tokenSource.Token;
      Task.Factory.StartNew(() =&gt;
          {
              while (true)
              {
                  if (token.IsCancellationRequested)
                  {
                      Console.WriteLine("Stopping actions");
                      return;
                  }
                  // Simulating some actions
                  Console.Write("*");
              }
          }, token);
      if (Console.ReadKey().KeyChar == 'c')
      {
          tokenSource.Cancel();
          Console.WriteLine("Stop command");
      }
      Console.WriteLine("Finished");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>What I'm looking for is some sort of host utility to control this application - spawn multiple instances and perform required user actions on each instance.
  Host application <code>Program.cs</code> :<br/></p>
  
  <pre><code>static void Main()
  {
      const string exe = "Child.exe";
      var exePath = System.IO.Path.GetFullPath(exe);
      var startInfo = new ProcessStartInfo(exePath)
      {
          RedirectStandardOutput = true,
          RedirectStandardInput = true,
          WindowStyle = ProcessWindowStyle.Hidden,
          WindowStyle = ProcessWindowStyle.Maximized,
          CreateNoWindow = true,
          UseShellExecute = false
      };
  
      var childProcess = new Process { StartInfo = startInfo };
      childProcess.OutputDataReceived += readProcess_OutputDataReceived;
      childProcess.Start();
      childProcess.BeginOutputReadLine();
      Console.WriteLine("Waiting 5s for child process to start...");
      Thread.Sleep(5000);
  
      Console.WriteLine("Enter input");
      var msg = Console.ReadLine();
      // Sending input parameter
      childProcess.StandardInput.WriteLine(msg);
      // Sending start command aka any key
      childProcess.StandardInput.Write("s");
      // Wait 5s while child application is working
      Thread.Sleep(5000);
      // Issue stop command
      childProcess.StandardInput.Write("c");
      // Wait for child application to stop
      Thread.Sleep(20000);
      childProcess.WaitForExit();
      Console.WriteLine("Batch finished");    
      Console.ReadLine();
  }
  </code></pre>
  
  <p>When I run this tool, after first input it crashes with "has stopped working" error and prompt to send memory dump to Microsoft. Output window in VS shows no exceptions.<br/>
  Guess this problem occurs somewhere between applications and may be because of output stream buffer overflow (child app is writing a lot of stars each second which mimics real output which may be huge) and I yet have no idea how to fix it. I don't really need to pass child's output to host (only send start-stop commands to child), but commenting RedirectStandardOutput and OutputDataReceived doesn't fix this problem. Any ideas how to make this work?</p>