Am working with C#'s TaskFactory using ContinueWith function. The issue am trying to solve it this
- Execute Foo().
- If result succeeded, exit
- If Foo() did not result in success, then iterate and execute Foo() until it results in success (max iterations 3)
- If it doesn't succeed in 3 iterations, give up
The code I started with looks like this
var executeTask = Task.Factory.StartNew<ExecutionStatus>(() =>
{
return Foo();
});
executeTask.ContinueWith(task => CheckIfExecutionWasSuccessful(task)).
ContinueWith(task => CheckIfExecutionWasSuccessful(task)).
ContinueWith(task => CheckIfExecutionWasSuccessful(task)).
ContinueWith(task => CheckLastTimeBeforeGivingUp(task));
Foo() and CheckIfExecutionWasSuccessful() looks like this
ExecutionStatus Foo(){
//Do my work
return new ExecutionStatus(){Succeded = true}
}
ExecutionStatus CheckIfExecutionWasSuccessful(Task<ExecutionStatus> task){
if(task.Result.Succeeded) return task.Result;
else return Foo()
Something tells me that this is not the best way to go about this problem. Any suggestions, ideas?