Quando si attende Task utilizzando Task.WaitAll e si specifica un timeout se TimeAll è scaduto, devo anche osservare separatamente eventuali attività non completate (ad esempio registrando una continuazione)?Task e Task.WaitAll con gestione delle eccezioni di timeout
This thread mi porta a pensare che la risposta sia sì, ma non ho trovato nient'altro che lo confermi.
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);
}
Quante eccezioni sono presenti in AggregateException che si verificano dopo WaitAll? –
Qui la semplice sorgente di lib completa: http://stackoverflow.com/questions/11831844/unobservedtaskexception-being-throw-but-it-is-handled-by-a-taskscheduler-unobser –