Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Scheduler.cs
1using System;
3using System.Threading;
4using System.Threading.Tasks;
5using Waher.Events;
6
8{
13 public class Scheduler : IDisposable
14 {
15 private static readonly TimeSpan OnlyOnce = TimeSpan.FromMilliseconds(-1);
16 private static readonly TimeSpan OneDay = TimeSpan.FromDays(1);
17
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;
22
27 public Scheduler()
28 {
29 }
30
34 public void Dispose()
35 {
36 if (!this.disposed)
37 {
38 this.disposed = true;
39
40 this.timer?.Dispose();
41 this.timer = null;
42
43 this.events.Clear();
44 }
45 }
46
54 public DateTime Add(DateTime When, Action<object> Callback, object State)
55 {
56 bool Utc = When.Kind == DateTimeKind.Utc;
57
58 if (Utc)
59 When = When.ToLocalTime();
60
61 lock (this.events)
62 {
63 while (this.events.ContainsKey(When))
64 When = When.AddTicks(this.gen.Next(1, 10));
65
66 this.events[When] = new ScheduledEvent(When, Callback, State);
67 this.RecalcTimerLocked();
68 }
69
70 if (Utc)
71 When = When.ToUniversalTime();
72
73 return When;
74 }
75
83 public DateTime Add(DateTime When, Func<object, Task> Callback, object State)
84 {
85 bool Utc = When.Kind == DateTimeKind.Utc;
86
87 if (Utc)
88 When = When.ToLocalTime();
89
90 lock (this.events)
91 {
92 while (this.events.ContainsKey(When))
93 When = When.AddTicks(this.gen.Next(1, 10));
94
95 this.events[When] = new ScheduledEvent(When, Callback, State);
96 this.RecalcTimerLocked();
97 }
98
99 if (Utc)
100 When = When.ToUniversalTime();
101
102 return When;
103 }
104
105 private void RecalcTimerLocked()
106 {
107 this.timer?.Dispose();
108 this.timer = null;
109
110 LinkedList<DateTime> ToRemove = null;
111 DateTime Now = DateTime.Now;
112 TimeSpan TimeLeft;
113
114 foreach (KeyValuePair<DateTime, ScheduledEvent> Event in this.events)
115 {
116 TimeLeft = Event.Key - Now;
117 if (TimeLeft <= TimeSpan.Zero)
118 {
119 if (ToRemove is null)
120 ToRemove = new LinkedList<DateTime>();
121
122 ToRemove.AddLast(Event.Key);
123
124 Task.Run(Event.Value.Execute);
125 }
126 else
127 {
128 if (TimeLeft < OneDay)
129 this.timer = new Timer(this.TimerElapsed, null, TimeLeft, OnlyOnce);
130 else
131 this.timer = new Timer(this.TimerElapsed, null, OneDay, OnlyOnce);
132
133 break;
134 }
135 }
136
137 if (!(ToRemove is null))
138 {
139 foreach (DateTime TP in ToRemove)
140 this.events.Remove(TP);
141 }
142 }
143
144 private void TimerElapsed(object P)
145 {
146 try
147 {
148 lock (this.events)
149 {
150 this.RecalcTimerLocked();
151 }
152 }
153 catch (Exception ex)
154 {
155 Log.Exception(ex);
156 }
157 }
158
162 public void Clear()
163 {
164 try
165 {
166 lock (this.events)
167 {
168 this.events.Clear();
169 this.RecalcTimerLocked();
170 }
171 }
172 catch (Exception ex)
173 {
174 Log.Exception(ex);
175 }
176 }
177
186 public bool Remove(DateTime When)
187 {
188 if (When.Kind == DateTimeKind.Utc)
189 When = When.ToLocalTime();
190
191 lock (this.events)
192 {
193 if (this.events.Remove(When))
194 {
195 this.RecalcTimerLocked();
196 return true;
197 }
198 }
199
200 return false;
201 }
202
203 }
204}
Class representing an event.
Definition: Event.cs:11
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
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.
Definition: Log.cs:1657
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
Definition: Scheduler.cs:14
bool Remove(DateTime When)
Removes an event scheduled for a given point in time.
Definition: Scheduler.cs:186
Scheduler()
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
Definition: Scheduler.cs:27
void Dispose()
IDisposable.Dispose
Definition: Scheduler.cs:34
DateTime Add(DateTime When, Func< object, Task > Callback, object State)
Adds an event.
Definition: Scheduler.cs:83
DateTime Add(DateTime When, Action< object > Callback, object State)
Adds an event.
Definition: Scheduler.cs:54
void Clear()
Clears all pending events.
Definition: Scheduler.cs:162