I am trying to understand data parallelism and processes in C#(Reference) and am getting a bit confused by the behavior. I may be missing some really basic concept , but it would be great if anyone could explain this
So I have the following code :-
private static void Process()
{
Process _process = new Process();
_process.StartInfo.FileName = "C:\\Windows\\notepad.exe";
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.RedirectStandardOutput = true;
// Start the process
_process.Start();
_process.BeginOutputReadLine();
Task task = Task.Factory.StartNew(() => MonitorProcess());
Console.WriteLine("Main ");
}
private static void MonitorProcess()
{
Console.WriteLine("Inside Monitor");
string x = null;
x = x.Trim();
}
Now my expectation is that "Main" should never be printed to the console since there is a null reference exception in the MonitorProcess method . I don't want to use task.Wait() to catch and throw the exception since that will wait for the task to complete.
So why does this happen and how can i get around this so that code execution is stopped at the exception in the MonitorProcess method ?