2

I am a programming teacher and I am developing an automated checker for the students HWs (All are Console Apps). Basically, I run the student code using some Process.Start() (with some input file) and check if its output is as expected. If, for some reason, the student code did no complete succesfully, I send them the exception thrown by their code using the process Standart Error redirection.

However, I want to do better then that. I want to find the input line written to the process StandartInput afterwhich their exception took place. How can I do that? My current code is more or less:

ProcessStartInfo psi = new ProcessStartInfo(students_exe_path);
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

String randomInputFile = randomInputFilesFolder + "\\" + "test.txt";
psi.WorkingDirectory = randomInputFilesFolder;
Process p = Process.Start(psi);
StreamWriter inputWriter = p.StandardInput;
String[] inputLines = File.ReadAllLines(randomInputFile);
foreach (String line in inputLines)
{
    // here I wanted to read the standart error so far (before writing next input line)
    String errorTextBefore = p.StandardError.ReadToEnd(); // can not use this because blocks progress.

    inputWriter.WriteLine(line); // sending next input line to students app

    // here I wanted to read the standart error so far (after writing next input line)
    String errorTextAfter = p.StandardError.ReadToEnd(); // can not use because blocks
    if (errorTextBefore != errorTextAfter) Console.WriteLine("Line {0} caused the exception",line);
}

Or maybe I can check from p.StandartInput which text was not "consumed" by the process?

Tamir

user2795947
  • 53
  • 1
  • 5
  • 1
    You can only read synchronous from one stream. To read from both Error and standard stream you need to read async and subscribe to the events. See: http://stackoverflow.com/a/26687938/578411 or http://stackoverflow.com/a/39318233/578411 – rene Oct 06 '16 at 10:37
  • https://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginerrorreadline(v=vs.110).aspx – Hans Passant Oct 06 '16 at 13:11

0 Answers0