public class MyJobRed : IJob
{
public void Execute()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString("yyyy MM dd HH:mm:ss"));
Console.ResetColor();
}
}
public class MyAsyncJob : IAsyncJob
{
public Task ExecuteAsync()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(DateTime.Now.ToString("yyyy MM dd HH:mm:ss"));
Console.ResetColor();
return Task.CompletedTask;
}
}
services.AddScheduler(schedule =>
{
schedule.CreateAsyncJobService<MyAsyncJob>(cfg => cfg.ToRunOnceIn(2).Seconds().AtStartTime());
schedule.CreaJobService<MyJobRed>(cfg => cfg.ToRunOnceIn(2).Seconds().AtStartTime());
schedule.CreaJobService<MyJobGreen>(cfg => cfg.ToRunOnceIn(3).Seconds().AtStartTime());
});
var provider = services.BuildServiceProvider();
var scheduler = provider.GetService<IScheduler>();
scheduler.StartAsync();
IAsyncJobService asyncJobService = JobManager.Scheduler()
.ToRunOnceIn(1)
.Seconds()
.AtStartTime()
.BuildAsyncJobService<MyAsyncJob>();
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
await asyncJobService.StartAsync(token);
IJobService myJobService = JobManager.Scheduler()
.ToRunOnceIn(5)
.Seconds()
.AtStartTime()
.BuildJobService<MyJob>();
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
myJobService.Start(token);