DispatchR is a lightweight and performant C# .NET library that dispatches asynchronous functions in order at a specified frequency or date time.
If you like or are using this project please give it a star. Thanks!
Dispatcher dispatcher = new Dispatcher(new Dispatchee2((token) =>
{
return MethodAsync(token); // Make some asynchronous call.
}, DispatchTime.AtMinute(1))); // Will execute at the first minute of every hour.
dispatcher.RunAsync(token); // Run the dispatcher until the CancellationTokenSource is cancelled.
Dispatcher dispatcher = new Dispatcher(new Dispatchee2((token) =>
{
return MethodAsync(token); // Make some asynchronous call.
}, DispatchTime.AtFrequency(TimeSpan.FromSeconds(10)))); // Will execute every 10 seconds.
dispatcher.RunAsync(token); // Run the dispatcher until the CancellationTokenSource is cancelled.
Dispatcher dispatcher = new Dispatcher(new Dispatchee[]
{
new Dispatchee1((token) =>
{
return MethodAsync(token); // Make some asynchronous call.
}, DispatchTime.AtMinute(1)), // Will execute on the first minute of every hour.
new Dispatchee2((token) =>
{
return MethodAsync(token); // Make some asynchronous call
}, DispatchTime.AtFrequency(TimeSpan.FromSeconds(10))) // Will execute every 10 seconds.
});
dispatcher.RunAsync(token); // Run the dispatcher until the CancellationTokenSource is cancelled.
// Will execute Dispatchee1 first.
[DispatchOrder(1)]
public class Dispatchee1 : Dispatchee
{
private readonly Func<CancellationToken, Task> _func;
public MockDispatchee1(Func<CancellationToken, Task> func, DispatchTime dispatchTime) : base(dispatchTime)
{
_func = func;
}
public override Task ExecuteAsync(CancellationToken token = default) => _func(token);
}
// Will execute Dispatchee2 second.
[DispatchOrder(2)]
public class Dispatchee2 : Dispatchee
{
private readonly Func<CancellationToken, Task> _func;
public MockDispatchee2(Func<CancellationToken, Task> func, DispatchTime dispatchTime) : base(dispatchTime)
{
_func = func;
}
public override Task ExecuteAsync(CancellationToken token = default) => _func(token);
}