Aggiunto progetto gestione TaskScheduler

This commit is contained in:
Samuele E. Locatelli
2020-03-12 11:27:15 +01:00
parent 334127ab89
commit 149d607ad3
10 changed files with 510 additions and 90 deletions
+42
View File
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace Steamware.Scheduler
{
/// <summary>
/// Classe di gestione Scheduler scadenziabili
/// </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);
}
}
}