c# - Task/Method scheduling from within a timer -


from within windows service cast method once hour. polling timer exists use rather adding "windows task".

in timer callback, checking whether call method or not following code, _config.pollinginterval interval of timer.

if (datetime.now.subtract(new datetime(datetime.now.year, datetime.now.month, datetime.now.day, datetime.now.hour, 0, 0)) < timespan.frommilliseconds(_config.pollinginterval)) {     sendreport(); } 

for reason, condition met twice within same minute (e.g. 08:00). guess there logical error somewhere, since it's assured there one timer.

any hints working or different/more elegant approach?

maybe this:

class reporter {     int _hourlastrun;      public reporter()     {         _hourlastrun = -1;     }      public void sendifneeded()     {         var currenthour = datetime.now.hour;          if(currenthour != _hourlastrun)         {             _hourlastrun = currenthour;             sendreport();         }     } } 

then instantiate class create timer , put sendifneeded() callback of timer.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -