I have a function that is called by several GUI components, and always needs to be run in a different Task.
Is there a difference between doing it in this way each time:
public int foo()
{
// do what you need to do
return 1;
}
...
var t = Task<int>.Factory.StartNew(() =>foo());
and starting the task inside the foo function?
public int foo()
{
var t = Task<int>.Factory.StartNew(() =>
{
// do what you need to do
return 1;
});
}
...
foo();
I get System.AggregateException in this program, and I’m starting to think that the second way to start the task is a wrong one.
Have a look at the InnerException. The
AggregateExceptionis used to bundle up exceptions caused by Tasks/PLinq. More info here:http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx
Worth also reading How to: Handle Exceptions Thrown by Tasks