The VersaTul Task Scheduler project provides functionality to schedule events then listen to and react to those events. This is ideal for windows service type applications that may run tasks based on certain time or day. Events can be scheduled from seconds, minutes, hours, days and many more combinations.
$ dotnet add package VersaTul.Task.SchedulerVersaTul Task Scheduler is a project that provides functionality to schedule events then listen to and react to those events. This is ideal for windows service type applications that may run tasks based on certain time or day.
To use VersaTul Task Scheduler, first install it using nuget:
Install-Package VersaTul.TaskScheduler
VersaTul Task Scheduler consists of several classes that represent different types of events and timers. Here is a brief overview of each class:
To use these classes, you need to create an instance of the ScheduleTimer class and add events to it using the AddEvent method. You can also use the AddSyncronized method to add events that operate asynchronously. Then you need to call the Start method to begin executing the assigned tasks at the scheduled times.
Here is a simple example of using ScheduleTimer in a BackgroundService:
public class MyService : BackgroundService
{
private readonly ScheduleTimer _timer;
public MyService()
{
_timer = new ScheduleTimer();
_timer.AddEvent(new ScheduledEvent(Daily, new TimeSpan(18, 0, 0)), OnDailyEvent);
_timer.AddEvent(new ScheduledEvent(Hourly, new TimeSpan(0, 15, 0)), OnHourlyEvent);
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_timer.Start();
return Task.CompletedTask;
}
private void OnDailyEvent(object sender, TimerEventArgs e)
{
// Do something every day at 6:00 PM
}
private void OnHourlyEvent(object sender, TimerEventArgs e)
{
// Do something every hour at 15 minutes past the hour
}
}This project is licensed under the MIT License - see the LICENSE file for details.