task parallel library - Running a polling job in a windows service with TPL -


in past have used timer polling job retrieves data 1 database , insert after processing data. i'm trying write tpl (task parallel library).

is following code task.delay, efficient way run polling job in window service tpl?

when said 'efficient' means doesn't consume more resources os neither wasting. jeffrey ritcher in wintellect video on thread fundamentals discourages use thread.sleep. argument app should not hold thread if not using other apps or same app can use thread pool.

i assume task.delay internally doing thread sleep assume inefficient.

another requirement job has run every minute , overlapping should avoided.

class program {     private cancellationtokensource _cancellationtokensource;     private task _etltask;      protected override void onstart(string[] args)     {         _cancellationtokensource = new cancellationtokensource();          _etltask = task.run(             async () =>                     {                         cancellationtoken token = tokensource.token;                          while (!token.iscancellationrequested)                         {                             await etljob.run(); // etl job read db update db                              await task.delay(timespan.fromminutes(1), token);                         }                     });     }      protected override void onstop()     {         _cancellationtokensource.cancel();          try         {             _etltask.wait();         }         catch (exception e)         {             // handle exeption         }     } } 

yes, code looks fine.

this job has run every minute , overlapping should avoided.

the way it's structured, has minute of delay between executions, not quite same thing "running every minute". if that's enough you, i'd keep as-is.


Comments