The TPL has a number of TaskContinuationOptions
values that control what circumstances a task executes under. For example, TaskContinuationOptions.NotOnCanceled
prevents a task from running when its parent is canceled.
However, none of these task state filters apply to multi-task continuations. You can't do something like:
TaskFactory f = new TaskFactory();
Task t1 = new Task (() => Thread.Sleep (5000));
Task t2 = new Task (() => Thread.Sleep (4000));
Task t3 = f.ContinueWhenAll (new Task[] { t1, t2 },
(tasks) => { ... },
TaskContinuationOptions.OnlyOnRanToCompletion);
You end up getting an error that says, "It is invalid to exclude specific continuation kinds for continuations off of multiple tasks."
What I don't understand is why this condition would be excluded from the API. Why wouldn't it be a perfectly valid use case to want a task to run only when all ante开发者_C百科cedents ended in a particular state?
ContinueWhenAll
means "run the continuation when all tasks have completed successfully". Specifying NotOn*
or OnlyOn*
would be either contrary or superfluous to that definition. See the remarks section of this MSDN article.
"...to run only when all antecedents ended in a particular state..."
Note your word "all", I'm not MS, but I'll bet it has to do with the fact that another whole TaskContinuationOptions
enum would be needed that included All
, Any
, OnlyOne
, AllButOne
and so on and so forth.
Furthermore, Eric Lippert is always answering questions like these with "it's expensive and time consuming to add a "simple" feature. Way moreso, than getting the basics right and letting users implement the rest.
精彩评论