5

I have an acceptance runner program here that looks something like this:

public Result Run(CommandParser parser)
{
    var result = new Result();
    var watch = new Stopwatch();

    watch.Start();

    try
    {
        _testConsole.Start();

        parser.ForEachInput(input =>
        {
            _testConsole.StandardInput.WriteLine(input);
            return _testConsole.TotalProcessorTime.TotalSeconds < parser.TimeLimit;
        });

        if (TimeLimitExceeded(parser.TimeLimit))
        {
            watch.Stop();
            _testConsole.Kill();
            ReportThatTestTimedOut(result);
        }
        else
        {
            result.Status = GetProgramOutput() == parser.Expected ? ResultStatus.Passed : ResultStatus.Failed;
            watch.Stop();
        }
    }
    catch (Exception)
    {
        result.Status = ResultStatus.Exception;
    }

    result.Elapsed = watch.Elapsed;
    return result;
}

the _testConsole is an Process adapter that wraps a regular .net process into something more workable. I do however have a hard time to catch any exceptions from the started process (i.e. the catch statement is pointless here) I'm using something like:

_process = new Process
                           {
                               StartInfo =
                                   {
                                       FileName = pathToProcess,
                                       UseShellExecute = false,
                                       CreateNoWindow = true,
                                       RedirectStandardInput = true,
                                       RedirectStandardOutput = true,
                                       RedirectStandardError = true,
                                       Arguments = arguments
                                   }
                           };

to set up the process. Any ideas?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
kitofr
  • 751
  • 1
  • 7
  • 11

1 Answers1

19

Exceptions don't flow from one process to another. The best you could do would be to monitor the exit code of the process - conventionally, an exit code of 0 represents success, and any other exit code represents an error.

Whether that's the case for the processes you're launching is a different matter, of course.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Well, shouldn't there be a way of supressing the exceptions that is thrown from a child process? – kitofr Nov 26 '08 at 14:45
  • 6
    Exceptions aren't thrown from one process to another at all. If a process throws an exception, it will either catch it somewhere or die. It's entirely local to the process. – Jon Skeet Nov 26 '08 at 15:07
  • 1
    Jon's right. If you need propagation, get the returning value, and if it's outside the correct range, throw the Exception yourself. – lb. Aug 19 '09 at 07:30
  • you could use interprocess-communication for an executable which is implemented by you https://learn.microsoft.com/en-us/windows/win32/ipc/interprocess-communications – user1911091 Jan 30 '20 at 09:19