MSDN Example of handling an exception from the TPL - Is this a race condition?

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-04-16T20:07:53Z Indexed on 2010/04/16 20:13 UTC
Read the original article Hit count: 458

Filed under:
|
|

I'm looking at the TPL exception handling example from MSDN @

http://msdn.microsoft.com/en-us/library/dd537614(v=VS.100).aspx

The basic form of the code is:

Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); });
try
{
   task1.Wait();
}
catch (AggregateException ae)
{
   throw ae.Flatten();
}

My question is: Is this a race condition? What happens if task1 throws before the try has executed? Am I missing something that stops this being a race?

Shouldn't it be written like this instead:

try
{
   Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); });
   task1.Wait();
}
catch (AggregateException ae)
{
   throw ae.Flatten();
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about TPL