Class SchedulerService
A service for scheduling tasks to run with a timed delay and/or repeat with a regular interval.
[ServiceBinding(typeof(IUpdateable))]
[ServiceBinding(typeof(SchedulerService))]
public sealed class SchedulerService : IUpdateable, IDisposable
- Inheritance
-
SchedulerService
- Implements
- Inherited Members
- Extension Methods
Examples
/*
* Usage examples for the Scheduler Service.
*/
using System;
using Anvil.Services;
namespace NWN.Anvil.Samples.Services
{
[ServiceBinding(typeof(ScheduledService))]
public class ScheduledService
{
private readonly IDisposable schedule;
private IDisposable runLater;
private int timesRun;
private readonly SchedulerService schedulerService;
public ScheduledService(SchedulerService schedulerService)
{
this.schedulerService = schedulerService;
// Schedules a repeating task to run every minute.
schedule = schedulerService.ScheduleRepeating(OncePerMinute, TimeSpan.FromMinutes(1));
// Schedules a once-off task to run in 20 minutes.
runLater = schedulerService.Schedule(RunIn20Minutes, TimeSpan.FromMinutes(20));
}
private void OncePerMinute()
{
timesRun++;
if (timesRun > 10)
{
// Cancel the repeating task after 10 runs.
schedule.Dispose();
// Actually, don't run the "RunLater" task in 20 minutes. Run it in 30 instead.
runLater.Dispose();
runLater = schedulerService.Schedule(RunIn20Minutes, TimeSpan.FromMinutes(30));
}
}
private void RunIn20Minutes()
{
// Do something
}
}
}
Fields
- NextUpdate
The next server loop after the current.
Methods
- Schedule(Action, TimeSpan)
Schedules the specified action to be invoked after the given delay.
- ScheduleRepeating(Action, TimeSpan, TimeSpan)
Schedules the specified task to be invoked on a specified schedule, after an optional delay.