Calling methods with delay¶

Sometimes you may want to postpone a method invocation; for example, to send an email to newly registered users a day after their registration. To do this, just call the BackgroundJob.Schedule method and pass the desired delay:

  1. BackgroundJob.Schedule(
  2. () => Console.WriteLine("Hello, world"),
  3. TimeSpan.FromDays(1));

Hangfire Server periodically checks the schedule to enqueue scheduled jobs to their queues, allowing workers to execute them. By default, check interval is equal to 15 seconds, but you can change it by setting the SchedulePollingInterval property on the options you pass to the BackgroundJobServer constructor:

  1. var options = new BackgroundJobServerOptions
  2. {
  3. SchedulePollingInterval = TimeSpan.FromMinutes(1)
  4. };
  5.  
  6. var server = new BackgroundJobServer(options);

If you are processing your jobs inside an ASP.NET application, you should perform the following steps to ensure that your scheduled jobs get executed at the correct time:

原文:

http://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html