How to catch exceptions from processes in C#
Posted
by kitofr
on Stack Overflow
See other posts from Stack Overflow
or by kitofr
Published on 2008-11-26T13:59:41Z
Indexed on
2010/05/09
20:38 UTC
Read the original article
Hit count: 205
I all... 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?
© Stack Overflow or respective owner