0

Given something like this:

.ContinueWith((_, o) => this.Foo(), null, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith((_, o) => this.Bar(), null, TaskContinuationOptions.OnlyOnFaulted)
.ContinueWith((_, o) => this.AnythingElse(), null, TaskContinuationOptions.?

Is there any way to construct a continuation that will execute AnythingElse when the other continuations have not executed?

I want to catch all other possible terminations, but only if one of the first two continuations above have not executed already.

Jim
  • 14,952
  • 15
  • 80
  • 167
  • if I understand correctly, you want to execute `AnythingElse`only if Foo or Bar didn't finish yet ? It's not very clear to me because you use `ContinueWith` (which means: run _this_ after the previous task completed), but you say you want to continue only if they haven't executed already – Fabio Salvalai Oct 05 '15 at 10:24
  • I want anythingelse to execute if there was no exception and it didn't run to completion - more importantly - under any circumstances that foo and bar were not executed. – Jim Oct 05 '15 at 10:48
  • _I want anythingelse to execute if there was no exception and it didn't run to completion_ how can it not run to completion ? do you want to set a timeout ? I'm not exactly sure what you mean here. – Fabio Salvalai Oct 05 '15 at 11:59

1 Answers1

0

Use OnlyOnCancelled because that is the only other case.

You also can use one unified continuation and decide what to do based on task status. I think that's the cleanest way because it results in sequential looking code.

usr
  • 168,620
  • 35
  • 240
  • 369
  • I'm afraid just using `OnlyOnCanceled` on the current piece of code will not work in case it's `Foo` who gets canceled. – Fabio Salvalai Oct 05 '15 at 13:07
  • If Foo is cancelled neither the antecedent (not shown here) nor the ContinueWith task will end up in a cancelled state. – usr Oct 05 '15 at 14:43