Reputation: 41
I'm currently writing a small editor for an interpreted programming language. When the program is run, the editor (written in C#) creates a new process that fires the interpreter (written in C++). A console appears like it would with any other C++ program, showing the output of the program.
When the interpreter (ie, the C++ program) encounters an error in the code, a message is printed to standard error, indicating the error type and line number on which it occurred. What I would like to do is read the interpreter's standard 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 standard error), somehow results in the standard output of the program not being printed to the console as well!
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 not to be written. Otherwise, the program behaves entirely as expected. Is there a way to make the standard output still write to the console, while still reading standard error?
Or is there a better way to get error indications from the interpreter process?
Upvotes: 4
Views: 5236
Reputation: 1937
I reckon you can achieve what you like if you subscirbe to both events (OutputDataReceived and ErrorDataReceived), then simple write out the 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.
Upvotes: 6