4using System.Threading.Tasks;
15 private static readonly TimeSpan OnlyOnce = TimeSpan.FromMilliseconds(-1);
16 private static readonly TimeSpan OneDay = TimeSpan.FromDays(1);
18 private readonly SortedDictionary<DateTime, ScheduledEvent> events =
new SortedDictionary<DateTime, ScheduledEvent>();
19 private readonly Random gen =
new Random();
20 private Timer timer =
null;
21 private bool disposed =
false;
40 this.timer?.Dispose();
54 public DateTime
Add(DateTime When, Action<object> Callback,
object State)
56 bool Utc = When.Kind == DateTimeKind.Utc;
59 When = When.ToLocalTime();
63 while (this.events.ContainsKey(When))
64 When = When.AddTicks(this.gen.Next(1, 10));
66 this.events[When] =
new ScheduledEvent(When, Callback, State);
67 this.RecalcTimerLocked();
71 When = When.ToUniversalTime();
83 public DateTime
Add(DateTime When, Func<object, Task> Callback,
object State)
85 bool Utc = When.Kind == DateTimeKind.Utc;
88 When = When.ToLocalTime();
92 while (this.events.ContainsKey(When))
93 When = When.AddTicks(this.gen.Next(1, 10));
95 this.events[When] =
new ScheduledEvent(When, Callback, State);
96 this.RecalcTimerLocked();
100 When = When.ToUniversalTime();
105 private void RecalcTimerLocked()
107 this.timer?.Dispose();
110 LinkedList<DateTime> ToRemove =
null;
111 DateTime Now = DateTime.Now;
114 foreach (KeyValuePair<DateTime, ScheduledEvent>
Event in this.events)
116 TimeLeft = Event.Key - Now;
117 if (TimeLeft <= TimeSpan.Zero)
119 if (ToRemove is
null)
120 ToRemove =
new LinkedList<DateTime>();
122 ToRemove.AddLast(
Event.Key);
124 Task.Run(
Event.Value.Execute);
128 if (TimeLeft < OneDay)
129 this.timer =
new Timer(this.TimerElapsed,
null, TimeLeft, OnlyOnce);
131 this.timer =
new Timer(this.TimerElapsed,
null, OneDay, OnlyOnce);
137 if (!(ToRemove is
null))
139 foreach (DateTime TP
in ToRemove)
140 this.events.Remove(TP);
144 private void TimerElapsed(
object P)
150 this.RecalcTimerLocked();
169 this.RecalcTimerLocked();
188 if (When.Kind == DateTimeKind.Utc)
189 When = When.ToLocalTime();
193 if (this.events.Remove(When))
195 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 ...
void Dispose()
IDisposable.Dispose
DateTime Add(DateTime When, Func< object, Task > Callback, object State)
Adds an event.
DateTime Add(DateTime When, Action< object > Callback, object State)
Adds an event.
void Clear()
Clears all pending events.