Scheduling with Timers

Component: NServiceBus
NuGet Package NServiceBus (8-pre)
This page targets a pre-release version and is subject to change prior to the final release.

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 =>
        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.

Last modified