c# - Using Task.Delay and a recursive call -


i'm trying figure out best way implement delay task such after delay calls again attempt same work.

my application server generates reports database after mobile devices sync data server, if user has called report generation method recently, want pause period of time , attempt run again.

this current attempt

private static datetime _lastrequest = datetime.minvalue;   public async void issuereports() {     await task.run(() =>     {         if (datetime.now < _lastrequest + timespan.fromminutes(3)) //checks see when user last completed method         {             task.delay(timespan.fromminutes(2));              issuereports(); //calls again after delay             return;         }     });      //code generate reports goes here     _lastrequest = datetime.now; //updates last request static variable after has finished running  } 

initially if failed check task end. prevented 2 users hitting database @ same time , causing duplicate reports generated. however, problem if 2 users sync within same window second user reports wouldn't sent until sync call done.

the delay supposed give server time finish generating reports , updating database before next batch requested calling itself.

am overcomplicating things? i'm worried potentially hammering system resources multiple loops in event reports take long time process

following example run background service every 10 seconds recursively. method recommended if believe task complete within 10 seconds.

public frm_testform() {      initializecomponent();      dispatchertimer_tick().donotawait();  }  private async task dispatchertimer_tick() {     dispatchertimer timer = new dispatchertimer();     taskcompletionsource<bool> tcs = null;     eventhandler tickhandler = (s, e) => tcs.trysetresult(true);      timer.interval = timespan.fromseconds(10);     timer.tick += tickhandler;     timer.start();      while (true)     {         tcs = new taskcompletionsource<bool>();          await task.run(() =>         {        // run background service , ui update here         await tcs.task;     }  } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -