c# - Running task in loop -


i have function can take 5-60 seconds run, , need run every 10 seconds should started when started function finished running, code is

action myaction = new action(() =>     {         debug.writeline("just testing");         thread.sleep(15000);     }); task mytask = task.factory.startnew(myaction, _cts.token); timer mytimer = new timer(state =>     {         if (mytask.iscompleted)         {             mytask = task.factory.startnew(myaction, _cts.token);         }     }, null, 10000, 10000); 

everything working fine wonder if there better solution problem? or there possibility not create new task (task.factory.startnew) using 1 used mytimer?

you can use continuewith():

task.factory.startnew(myaction, _cts.token).continuewith(_ => myaction); 

look it's overloads, has many options control on cases run continuation.


Comments