嗨,我有以下代码:
try
{
var t1 = Task.Factory.StartNew(() =>
{
Count(5, 10);
});
//t1.Wait(); //This line if uncommented causes the exception to be handled below....
Console.WriteLine("done");
}
catch (AggregateException ex)
{
Console.WriteLine(ex);
}
private void Count(int start, int end)
{
for (var i = start; i <= end; i++)
{
Console.WriteLine(i);
if (i == 7) throw new InvalidOperationException("Something bad happened");
Thread.Sleep(1000);
}
}
如果我不想等待我的任务,但仍想处理异常,我该如何实现?