2using System.Collections.Generic;
4using System.Threading.Tasks;
27 private static readonly TimeSpan OnlyOnce = TimeSpan.FromMilliseconds(-1);
28 private static readonly TimeSpan OneDay = TimeSpan.FromDays(1);
30 private readonly SortedDictionary<DateTime, ScheduledEvent> events =
new SortedDictionary<DateTime, ScheduledEvent>();
31 private readonly Random gen =
new Random();
32 private Timer timer =
null;
33 private bool disposed =
false;
52 this.timer?.Dispose();
70 while (this.events.ContainsKey(When))
71 When = When.AddTicks(this.gen.Next(1, 10));
73 this.events[When] =
new ScheduledEvent(When, Callback, State);
74 this.RecalcTimerLocked();
91 while (this.events.ContainsKey(When))
92 When = When.AddTicks(this.gen.Next(1, 10));
94 this.events[When] =
new ScheduledEvent(When, Callback, State);
95 this.RecalcTimerLocked();
101 private void RecalcTimerLocked()
103 this.timer?.Dispose();
106 LinkedList<DateTime> ToRemove =
null;
107 DateTime Now = DateTime.Now;
110 foreach (KeyValuePair<DateTime, ScheduledEvent>
Event in this.events)
112 TimeLeft = Event.Key - Now;
113 if (TimeLeft <= TimeSpan.Zero)
115 if (ToRemove is
null)
116 ToRemove =
new LinkedList<DateTime>();
118 ToRemove.AddLast(
Event.Key);
120 Task.Run(
Event.Value.Execute);
124 if (TimeLeft < OneDay)
125 this.timer =
new Timer(this.TimerElapsed,
null, TimeLeft, OnlyOnce);
127 this.timer =
new Timer(this.TimerElapsed,
null, OneDay, OnlyOnce);
133 if (!(ToRemove is
null))
135 foreach (DateTime TP
in ToRemove)
136 this.events.Remove(TP);
140 private void TimerElapsed(
object P)
146 this.RecalcTimerLocked();
165 this.RecalcTimerLocked();
186 if (this.events.Remove(When))
188 this.RecalcTimerLocked();
Class representing an event.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
bool Remove(DateTime When)
Removes an event scheduled for a given point in time.
Scheduler()
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
DateTime Add(DateTime When, ScheduledEventCallbackAsync Callback, object State)
Adds an event.
void Dispose()
IDisposable.Dispose
DateTime Add(DateTime When, ScheduledEventCallback Callback, object State)
Adds an event.
void Clear()
Clears all pending events.
delegate void ScheduledEventCallback(object State)
Callback method for scheduled events.
delegate Task ScheduledEventCallbackAsync(object State)
Callback method for asynchronous scheduled events.