Task IHostedService.StartAsync(CancellationToken cancellationToken) { // timer repeates call to CreateTimeSlots every 24 hours. TimeSpan interval = TimeSpan.FromHours(24); //.FromMinutes(1);// //calculate time to run the first time & delay to set the timer //DateTime.Today gives time of midnight 00.00 var nextRunTime = DateOperations.Today().AddDays(1).AddHours(7); // DateTime.Now.AddMinutes(2);// var curTime = DateOperations.Now(); // DateTime.Now; var firstInterval = nextRunTime.Subtract(curTime); void action() { var t1 = Task.Delay(firstInterval, cancellationToken); t1.Wait(cancellationToken); //create at expected time _logger.LogInformation("First service run"); CreateTimeSlots(null); //now schedule it to be called every 24 hours for future // timer repeates call to CreateTimeSlots every 24 hours. _timer = new Timer( CreateTimeSlots, null, TimeSpan.Zero, interval ); } // no need to await this call here because this task is scheduled to run much much later. Task.Run(action, cancellationToken); return(Task.CompletedTask); }
public static Task <List <TimeSlot> > CreateTodayTimeSlots(UULContext context, int hourToStart) => CreateTimeSlotsForDateUTC(context, DateOperations.Today(), hourToStart);