views:

35

answers:

2

If I have code such as

proc.Start();
string resultOut;

while ( (!proc.HasExited && (resultOut = stdOut.ReadLine()) != null))
{
// Do some operation based on resultOut
}

Am I liable to miss some lines from when I start proc to when the capturing/parsing begins or will it wait? If it doesn't what can I do?

+2  A: 

If you're redirecting the input and/or output of the process via ProcessStartInfo.RedirectStnadardOutput, etc, the process output will go directly to your streams. You won't miss any input or output.

Reed Copsey
+1  A: 

The following code will not lose any lines from stdout.

var startInfo = new ProcessStartInfo
{
    FileName = "my.exe",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};

using (var process = new Process { StartInfo = startInfo })
{
    process.ErrorDataReceived += (s, e) =>
    {
        string line = e.Data;            
        //process stderr lines

    };

    process.OutputDataReceived += (s, e) =>
    {
        string line = e.Data;
        //process stdout lines
    };

    process.Start();

    process.BeginErrorReadLine();
    process.BeginOutputReadLine();

    process.WaitForExit();
}
Bear Monkey