コード例 #1
0
 public void AddTask(ITimerService scheduledTask, DateTime?nextRunTime = null)
 {
     foreach ((var container, var method, var cron) in scheduledTask.GetType().GetMethods().SelectMany(m => m.GetCustomAttributes(typeof(CronAttribute), true).OfType <CronAttribute>().Select(c => (scheduledTask, m, c))))
     {
         if (!(cron.Schedule.StartsWith("* ") || cron.Schedule.StartsWith("0 ")))
         {
             throw new NotImplementedException();
         }
         _scheduledTasks.Add(new SchedulerTaskWrapper
         {
             Schedule    = CrontabSchedule.Parse(cron.Schedule.Substring(2).Replace('?', '*')),
             Container   = container,
             Task        = method,
             NextRunTime = nextRunTime ?? DateTime.Now
         });
     }
 }