WITH#; Redirecting a standard process error, but not its standard output

I am currently writing a small editor for an interpreted programming language. When the program starts, the editor (written in C #) creates a new process that launches the interpreter (written in C ++). The console looks as if it were with any other C ++ program that displays the output of the program.

When an interpreter (i.e. a C ++ program) encounters an error in the code, the message is printed with a standard error, indicating the type of error and the line number on which it occurred. What I would like to do is read the standard interpreter error from the editor, so the editor can highlight the error line as indicated in the error message.

Unfortunately, the code below (designed to read only the standard error) leads to the fact that the standard output of the program also does not print on the console!

private void indicateErrorTest(object sendingProcess, DataReceivedEventArgs outLine)
{
    MessageBox.Show(outLine.Data);
}

private void run()
{

    program = new Process();
    program.StartInfo.FileName = INTERPRETER_PATH;
    program.StartInfo.Arguments = "\"" + relativeFilename + "\"";

    program.StartInfo.RedirectStandardError = true;
    program.StartInfo.UseShellExecute = false;

    program.ErrorDataReceived += new DataReceivedEventHandler(indicateErrorTest);
    program.Start();

    program.BeginErrorReadLine();

    program.EnableRaisingEvents = true;
    program.Exited += new System.EventHandler(onProgramConsoleClose);

}

I'm not even sure what causes the output to not be written. Otherwise, the program behaves as expected. Is there any way to make standard output still write to the console, while maintaining a standard error? Or is there a better way to get error messages from the interpreter process? Thank.

+3
source share
1 answer

I believe that you can achieve what you like if you connect to both events (OutputDataReceived and ErrorDataReceived), and then just write standard output to the console, for example:

_process.OutputDataReceived += Process_OutputDataReceived;
_process.ErrorDataReceived += Process_ErrorDataReceived;

void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}

void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    // do your stuff
}

Hope this helps.

+6
source

Source: https://habr.com/ru/post/1795847/


All Articles