47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IOB_MAN8.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Classe di gestione Scheduler scadenziabili
|
|
///
|
|
/// rif https://codinginfinite.com/creating-scheduler-task-seconds-minutes-hours-days/
|
|
/// </summary>
|
|
public class SchedulerService
|
|
{
|
|
private static SchedulerService _instance;
|
|
private List<Timer> timers = new List<Timer>();
|
|
|
|
private SchedulerService() { }
|
|
|
|
public static SchedulerService Instance => _instance ?? (_instance = new SchedulerService());
|
|
|
|
public void ScheduleTask(int hour, int min, double intervalInHour, Action task)
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
DateTime firstRun = new DateTime(now.Year, now.Month, now.Day, hour, min, 0, 0);
|
|
if (now > firstRun)
|
|
{
|
|
firstRun = firstRun.AddDays(1);
|
|
}
|
|
|
|
TimeSpan timeToGo = firstRun - now;
|
|
if (timeToGo <= TimeSpan.Zero)
|
|
{
|
|
timeToGo = TimeSpan.Zero;
|
|
}
|
|
|
|
var timer = new Timer(x =>
|
|
{
|
|
task.Invoke();
|
|
}, null, timeToGo, TimeSpan.FromHours(intervalInHour));
|
|
|
|
timers.Add(timer);
|
|
}
|
|
}
|
|
}
|