Anvil
Anvil.Services.SchedulerService Class Reference

A service for scheduling tasks to run with a timed delay and/or repeat with a regular interval. More...

+ Inheritance diagram for Anvil.Services.SchedulerService:
+ Collaboration diagram for Anvil.Services.SchedulerService:

Public Member Functions

ScheduledTask Schedule (Action action, TimeSpan delay)
 Schedules the specified action to be invoked after the given delay. More...
 
ScheduledTask ScheduleRepeating (Action action, TimeSpan schedule, TimeSpan delay=default)
 Schedules the specified task to be invoked on a specified schedule, after an optional delay. More...
 

Static Public Attributes

static readonly TimeSpan NextUpdate = TimeSpan.Zero
 The next server loop after the current. More...
 

Detailed Description

A service for scheduling tasks to run with a timed delay and/or repeat with a regular interval.

Examples

/*
* Usage examples for the Scheduler Service.
*/
using System;
namespace NWN.Anvil.Samples
{
[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
}
}
}

Member Function Documentation

◆ Schedule()

ScheduledTask Anvil.Services.SchedulerService.Schedule ( Action  action,
TimeSpan  delay 
)
inline

Schedules the specified action to be invoked after the given delay.

Parameters
actionThe task/action to run.
delayThe delay until the task is run.
Returns
A disposable object representing the scheduled task. Calling Dispose() will prevent the schedule from running.
Exceptions
ArgumentOutOfRangeExceptionThrown if the delay is less than 0.
ArgumentNullExceptionThrown if no action is specified.

◆ ScheduleRepeating()

ScheduledTask Anvil.Services.SchedulerService.ScheduleRepeating ( Action  action,
TimeSpan  schedule,
TimeSpan  delay = default 
)
inline

Schedules the specified task to be invoked on a specified schedule, after an optional delay.

Parameters
actionThe task/action to be run.
scheduleThe delay between invocations.
delayAn additional delay for the first time run.
Returns
A disposable object representing the scheduled task. Calling Dispose() will cancel the schedule and any future invocations.
Exceptions
ArgumentOutOfRangeExceptionThrown if the schedule is less than 0.
ArgumentNullExceptionThrown if no action is specified.

Member Data Documentation

◆ NextUpdate

readonly TimeSpan Anvil.Services.SchedulerService.NextUpdate = TimeSpan.Zero
static

The next server loop after the current.


The documentation for this class was generated from the following file: