Khi đợi Tác vụ bằng Task.WaitAll và chỉ định thời gian chờ nếu WaitAll hết thời gian, tôi cũng phải theo dõi riêng các tác vụ chưa hoàn thành (ví dụ: bằng cách đăng ký tiếp tục)?Tác vụ và tác vụ.WaitAll với xử lý ngoại lệ hết giờ
This thread dẫn tôi nghĩ câu trả lời là có, nhưng tôi chưa tìm thấy bất kỳ điều gì khác xác nhận điều này.
var random = new Random();
var tasks = Enumerable.Range(0, 10).Select((i) => Task.Factory.StartNew(() => {
// Sleep 5-15 seconds
Thread.Sleep(random.Next(5000, 15000));
throw new Exception("Some exception.");
}
)).ToArray();
try {
// Wait for 10 seconds
Task.WaitAll(tasks, 10000);
} catch (AggregateException) {
// Swallow
}
// Check to see how many tasks were unfinished
tasks.Where(t => !t.IsCompleted).Count().Dump("Unfinished count: ");
// Is this neccessary to avoid exceptions being thrown on the finalizer?
foreach (Task task in tasks) {
task.ContinueWith(t => t.Exception, TaskContinuationOptions.OnlyOnFaulted);
}
Có bao nhiêu trường hợp ngoại lệ trong Tổng quan ngoại lệ mà bạn nắm bắt sau WaitAll? –
Nguồn lib đơn giản hoàn chỉnh tại đây: http://stackoverflow.com/questions/11831844/unobservedtaskexception-being-throw-but-it-is-handled-by-a-taskscheduler-unobser –