Search code examples
c#asynchronousasync-awaitcontinuations

executing ContinueWith when other continuations have not executed


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.


Solution

  • 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.