What I am trying to achieve is real-time(synchronous) redirection. I tried many options which are already answered in Stackoverflow. But EVERY answer only suggest async redirection. The word real-time means when slave process print '~~' to console, then master process application immediately capture this and print '~~' to its console.
Below is current code. Note that, I tried several ways and other approach are commented. And I think System.Diagnostics.Process only offer asynchronous stream. Any good solution to achieve synchronous stream in c#?
var processStartInfo = new ProcessStartInfo
{
FileName = "myprocess.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
var process = new Process();
process.StartInfo = processStartInfo;
process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
};
process.Start();
process.BeginOutputReadLine();
/* Below is other approach, which do not use DataReceivedEventHandler
string line;
do
{
line = process.StandardOutput.ReadLine();
// non-thread approach is also tried.
Thread thread = new Thread(delegate ()
{
Console.WriteLine(line);
});
thread.isBackGround = true;
thread.Start();
} while (line != null);
*/