I’m using Task continuations for a long running operation on a WinForm project.
var task1 = Task.Factory.StartNew(() => DoSomeWork());
var task2 = task1.ContinueWith(result => DoSomeMoreWork(), TaskContinuationOptions.OnlyOnRanToCompletion);
var task3 = task2.ContinueWith(result => DoFinalWork(), TaskContinuationOptions.OnlyOnRanToCompletion);
I only want to continue to task3 if some conditions are met on the DoSomeMoreWork() function that executes on task2. How can this be done?
From MSDN
So one way would be in task3, check the Result from task2 before doing any work.
Where the Result returned from task2 determines what happens in task3.
EDIT
Another solution would be to cancel task2 if certain conditions are not met within the operation. Then the continuation task is not even scheduled.
From MSDN
So task3 definition becomes