如果我在下面的示例中取消註釋Wait()
或startNew.Result
的行,我將捕獲AggregateException
。爲什麼我看不到任何例外,如果這些代碼行被評論?存儲任務內的異常在哪裏?
class Program
{
static void Main(string[] args)
{
try
{
Task<int> startNew = Task.Factory.StartNew(() => int.Parse(""),
TaskCreationOptions.LongRunning);
//int result = startNew.Result;
//startNew.Wait();
Thread.Sleep(10000);
}
catch (AggregateException ex)
{
Console.WriteLine("Catched in aggregated");
}
catch (Exception)
{
Console.WriteLine("Catched in general");
}
Console.WriteLine("The end");
Console.ReadLine();
}
}
所以我正確的是ThreadPool.QueueUserWorkItem()立即拋出異常,因爲沒有機制來獲得結果或等待完成? – EngineerSpock