Process class stdout and stderr duplicating

I have very strange behavior when trying to output results of gmake work using Process class. I do exactly as it is suggested in MSDN article and everything works fine until gmake founds some error and want to report it (for example it has line "include somefile.d" for dependency but the file does not exist). In that case the regular gmake behavior is report the error (i think to stderr) and try to find rule for creating the missing file. If it founds the rule the execution continues.
In my application i receive report about exception raised after the call to DuplicateHandle fails with error=6 (Invalid handle). If i run this process without redirecting stdout and stderr output everything goes OK. If i run make file that does not produce error output everything goes OK.
Does anybody knows secret tricks or patches to fix the situation. Maybe there is any custom implementation of the Process class or I should reinvent this class


Answer this question

Process class stdout and stderr duplicating

  • Brian2

    I'm not sure I understand your problem.  Are you saying this occurs in your app if you set ProcessStartInfo.RedirectStandardError to true and then call Process.Start   If so then I'd try another app.  I've redirected this stream just fine for many apps so I can't imagine why it might fail. What MSDN article did you use

    StandardError and StandardOut are separate streams that you can redirect as needed.  Errors sent to StandardErr may also appear in StandardOut since but that is an execution environment or program issue and not yours.  The biggest issue with redirecting the standard streams is that you'll have to use secondary threads because the Read methods will block until data appears.  Therefore once you call Read on StdErr your app will block until an error appears.  If no error appears then your app will wait indefinitely.  Even worse is the fact that the buffers used by the standard streams will themselves block once they get full so if you send a lot of data to StdOut then the process sending the data will block until you call Read on StdOut.  This is all documented in the help for Process.StandardOutput.  You can also find a KB article on it I believe.

    In .NET 2.0 MS has provided async versions of the Read method to work around these issues so if you are using .NET 2.0 you should use the async versions instead.

    Michael Taylor - 10/24/05

  • Nelson Xu

    The following is a line that gmake generates when run in the console:
    makefile.mak:125: myfile.rd: No such file or directory
    The following is the output i get into my window:
    makefile.mak:125: myfile.rd: No such file or directory
    process_easy: DuplicateHandle(In) failed (e=6)
    "gmake": Interrupt/Exception caught (code = 0xc0000005, addr = 0x412fd4)

    The code that starts the process execution is below:


    buildProc = new Process();
    buildProc.StartInfo.FileName = "gmake";
    buildProc.StartInfo.Arguments = "-r -f makefile.mak"; buildProc.StartInfo.RedirectStandardOutput = true;
    buildProc.StartInfo.RedirectStandardError = true;
    buildProc.StartInfo.UseShellExecute = false;
    buildProc.StartInfo.CreateNoWindow = true;
    buildProc.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
    buildProc.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
    buildProc.Exited += new EventHandler(ExitedHandler);
    buildProc.EnableRaisingEvents = true;
    buildProc.Start();
    buildProc.BeginOutputReadLine();
    buildProc.BeginErrorReadLine();

     

    DataReceivedEventHandler is defined as following:



    private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
       
    if (!String.IsNullOrEmpty(outLine.Data))
       {
          
    EventArgs args = new uildProcessOutput(outLine.Data);
          
    object[] list ={ this, args };

          BeginInvoke(
    new uildOutputEventsHandler(UpdateMainFormLog), list);
       }
    }

     

    DataReceivedEventArgs is class containing additional string Data field.



  • Process class stdout and stderr duplicating