Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Scheduling with Timers

Component: NServiceBus
NuGet Package: NServiceBus (8.x)

This sample illustrates the use of a .NET Timer to trigger scheduled tasks. To leverage the benefits of NServiceBus retries and consistency of outgoing messages with the transport transaction, the tasks are implemented as regular message handlers. This also gives full traceability of the invoked tasks in platform tools like ServicePulse and ServiceInsight.

var interval = TimeSpan.FromSeconds(5);

var timer = new Timer(async state =>
{
    try
    {
        await endpointInstance.SendLocal(new MyScheduledTask());
        log.Info(nameof(MyScheduledTask) + " scheduled");
    }
    catch (Exception ex)
    {
        log.Error(nameof(MyScheduledTask) + " could not be scheduled", ex);
    }
}, null, interval, interval);

Related Articles

  • Scheduling
    Schedule a task or an action/lambda, to be executed repeatedly at a given interval.