c# - How to run a Task N times until it succeeds? -


am working c#'s taskfactory using continuewith function. issue trying solve this

  • execute foo().
  • if result succeeded, exit
  • if foo() did not result in success, iterate , execute foo() until results in success (max iterations 3)
  • if doesn't succeed in 3 iterations, give up

the code started looks 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() , checkifexecutionwassuccessful() looks this

executionstatus foo(){       //do work       return new executionstatus(){succeded = true}      }  executionstatus checkifexecutionwassuccessful(task<executionstatus> task){             if(task.result.succeeded) return task.result;             else return foo() 

something tells me not best way go problem. suggestions, ideas?

i don't understand why making more complicated using multiple tasks , continuewith(). instead, write code without tasks , then run in task:

task.factory.startnew(() => {     (int = 0; < maxtries - 1; i++)     {         try         {             return foo();         }         catch         { }     }      return foo(); }); 

this makes code clearer, more correct , easier modify.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -