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);
*/
5
  • What's wrong with the asynchronous approach? You can block and wait for the event to arrive, making it synchronous. Commented Jul 3, 2017 at 11:11
  • Related: Realtime Console Output Redirection using Process Commented Jul 3, 2017 at 11:21
  • @zmbq biggest problem is i have to wait til 'slave' exit. Async event is called when 'slave' exited. Commented Jul 3, 2017 at 11:49
  • 1
    The slave should probably flush its output. Commented Jul 3, 2017 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. :) Commented Jul 4, 2017 at 2:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.