Files
Pilz/Pilz/Jobs/JobCenter.cs
Pilzinsel64 82b204aa1f fix
2025-10-22 07:53:53 +02:00

59 lines
1.2 KiB
C#

namespace Pilz.Jobs;
public class JobCenter
{
private readonly HashSet<Job> jobs = [];
private readonly System.Timers.Timer timerRepeat = new()
{
AutoReset = false,
Interval = 5000,
};
public bool Enabled { get; protected set; }
public JobCenter()
{
timerRepeat.Elapsed += TimerRepeat_Elapsed;
}
private void TimerRepeat_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var now = DateTime.Now;
var jobs = this.jobs.Where(n => n.LastExecution + n.Interval > now);
foreach (var job in jobs)
{
job.LastExecution = now;
job.Execute(new(this));
}
if (Enabled)
timerRepeat.Start();
}
public virtual void AddJob(Job job)
{
jobs.Add(job);
}
public virtual void RemoveJob(Job job)
{
jobs.Remove(job);
}
public virtual void Start()
{
var now = DateTime.Now;
foreach (var job in jobs)
job.LastExecution = now;
Enabled = true;
timerRepeat.Start();
}
public virtual void Stop()
{
Enabled = false;
timerRepeat.Stop();
}
}