执行'n'次后结束重复作业

时间:2017-04-19 09:42:36

标签: c# asp.net-mvc hangfire

我正在使用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'次后结束此重复工作?

1 个答案:

答案 0 :(得分:0)

执行此操作的最简单方法是在调用方法时传递特定次数,并防止在达到该数字后执行该操作:

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);