Как получить информацию, если задача успешно завершена, в следующем примере: приложение создает случайное число от 1 до 10 каждые 5 секунд.Если число равно 5, вернуть true, иначе false.Вот что я сделал до сих пор:
public static async void Operate()
{
CreateScheduler();
IJobDetail job = JobBuilder.Create<MyFirstJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 5 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
await _scheduler.ScheduleJob(job, trigger);
}
class MyFirstJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
bool retVal = false;
var myTask = new Task(() =>
{
try
{
Random rnd = new Random();
int num = rnd.Next(1, 11);
Console.WriteLine("Generated num:" + num);
if (num != 5)
{
if (context.RefireCount < 6)
{
Console.WriteLine("Failed!");
throw new NotImplementedException();
}
}
Console.WriteLine("OK");
retVal = true;
}
catch (Exception ex)
{
}
});
myTask.Start();
return myTask;
}
}
Это хороший подход?