2017-04-19 61 views
0

我使用的是Hangfire版本「1.6.8」。「n」次執行後結束重複作業

var datetime = DateTime.Now; 

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

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

如何在'n'次執行後終止該重複作業?

回答

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