2021年1月4日星期一

ASP.Net Scheduled Task Executing Method by Multiples

Credit to the author from this site:

https://codinginfinite.com/creating-scheduler-task-seconds-minutes-hours-days/

I can define the task execution parameter in 24hr time to run my method, but I see no logic in the code that is clear to me as to why it fires my method by a large multiplicity of times. The issue is that where my method should be executed only but once per minute starting at the specified time, it is actually executed 20+ times in less than one minute. I've tried altering parameters and repeat intervals, but nothing seems to resolve the issue and different execution intervals always fire the method many times over than what is specified - i.e. similarly, 5min interval, but I get 30+ order placements within one minute whereas I would expect not more than one order execution every 5 minutes..

Any ideas on why this could be happening with this code?

Here is the service class:

public class SchedulerService      {                          private static SchedulerService _instance;          private List<Timer> timers = new List<Timer>();          private SchedulerService() { }          public static SchedulerService Instance => _instance ?? (_instance = new SchedulerService());          public void ScheduleTask(int hour, int min, double intervalInHour, Action task)          {                          DateTime now = DateTime.Now;              DateTime firstRun = new DateTime(now.Year, now.Month, now.Day, hour, min, 0, 0);                if (now > firstRun)              {                  firstRun = firstRun.AddDays(1);              }                            TimeSpan timeToGo = firstRun - now;              if (timeToGo <= TimeSpan.Zero)              {                  timeToGo = TimeSpan.Zero;              }                               var timer = new Timer(x =>              {                  task.Invoke();              }, null, timeToGo, TimeSpan.FromHours(intervalInHour));              timers.Add(timer);                      }      }  

Here is the scheduler class:

public class Scheduler      {          public static void IntervalInSeconds(int hour, int sec, double interval, Action task)          {              interval = interval / 3600;              SchedulerService.Instance.ScheduleTask(hour, sec, interval, task);          }          public static void IntervalInMinutes(int hour, int min, double interval, Action task)          {              interval = interval / 60;              SchedulerService.Instance.ScheduleTask(hour, min, interval, task);          }          public static void IntervalInHours(int hour, int min, double interval, Action task)          {              SchedulerService.Instance.ScheduleTask(hour, min, interval, task);          }          public static void IntervalInDays(int hour, int min, double interval, Action task)          {              interval = interval * 24;              SchedulerService.Instance.ScheduleTask(hour, min, interval, task);          }      }  

Instantiated on page load with defined start time parameters and repeat interval:

    protected void Page_Load(object sender, EventArgs e)      {          Scheduler.IntervalInMinutes(20, 15, 1,          () => {              buyOrder();          });      }  

At 20:15, call this method and repeat every minute:

    private static void buyOrder()      {          //This is only a basic POST method      }  

Result:

I get the multiplicity of orders executed in <1min as mentioned.

https://stackoverflow.com/questions/65572614/asp-net-scheduled-task-executing-method-by-multiples January 05, 2021 at 10:55AM

没有评论:

发表评论