【问题标题】:End Recurring Job after ‘n’ times execute在“n”次执行后结束重复作业
【发布时间】:2017-04-19 18:34:47
【问题描述】:

我正在使用 Hangfire 版本“1.6.8”。

var datetime = DateTime.Now; 

var cron = Cron.Monthly(datetime.Day,datetime.Hour); 

RecurringJob.AddOrUpdate<IService>( recurringId, x =>x.CreateRecurring(id), cron);

如何在执行“n”次后结束此循环作业?

【问题讨论】:

    标签: c# asp.net-mvc hangfire


    【解决方案1】:

    最简单的方法是在调用方法时传入特定次数,并在达到该次数后阻止它执行:

    public class MyService : IService
    {
        public int runCount = 0;
    
        public void CreateRecurring(id, int? maxTimes = null)
        {
            if (maxTimes.HasValue && (runCount >= maxTimes))
            {
                // Has run enough times now, don't do it again
                return;
            }
    
            // do something...
        }
    }
    
    
    // Run a max of 5 times
    RecurringJob.AddOrUpdate<IService>( recurringId, x =>x.CreateRecurring(id, 5), cron);
    

    【讨论】:

      猜你喜欢
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2017-10-11
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      相关资源
      最近更新 更多