0

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);
*/
TAZO
  • 33
  • 1
  • 5
  • What's wrong with the asynchronous approach? You can block and wait for the event to arrive, making it synchronous. – zmbq Jul 03 '17 at 11:11
  • Related: [Realtime Console Output Redirection using Process](https://stackoverflow.com/q/2332127/5265292) – grek40 Jul 03 '17 at 11:21
  • @zmbq biggest problem is i have to wait til 'slave' exit. Async event is called when 'slave' exited. – TAZO Jul 03 '17 at 11:49
  • 1
    The slave should probably flush its output. – zmbq Jul 03 '17 at 12:24
  • @zmbq yep flush was key problem. When I change slave to flush immediately then this code capture in real-time thanks very much zmbq. :) – TAZO Jul 04 '17 at 02:18

0 Answers0