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:
- BackgroundJob.Schedule(
- () => Console.WriteLine("Hello, world"),
- 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:
- var options = new BackgroundJobServerOptions
- {
- SchedulePollingInterval = TimeSpan.FromMinutes(1)
- };
- 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:
- Disable Idle Timeout – set its value to 0.
- Use the application auto-start feature.
原文:
http://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html