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;
2using System.Collections.Generic;
3using System.Threading;
4using System.Threading.Tasks;
5using Waher.Events;
6
8{
13 public delegate void ScheduledEventCallback(object State);
14
19 public delegate Task ScheduledEventCallbackAsync(object State);
20
25 public class Scheduler : IDisposable
26 {
27 private static readonly TimeSpan OnlyOnce = TimeSpan.FromMilliseconds(-1);
28 private static readonly TimeSpan OneDay = TimeSpan.FromDays(1);
29
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;
34
39 public Scheduler()
40 {
41 }
42
46 public void Dispose()
47 {
48 if (!this.disposed)
49 {
50 this.disposed = true;
51
52 this.timer?.Dispose();
53 this.timer = null;
54
55 this.events.Clear();
56 }
57 }
58
66 public DateTime Add(DateTime When, ScheduledEventCallback Callback, object State)
67 {
68 lock (this.events)
69 {
70 while (this.events.ContainsKey(When))
71 When = When.AddTicks(this.gen.Next(1, 10));
72
73 this.events[When] = new ScheduledEvent(When, Callback, State);
74 this.RecalcTimerLocked();
75 }
76
77 return When;
78 }
79
87 public DateTime Add(DateTime When, ScheduledEventCallbackAsync Callback, object State)
88 {
89 lock (this.events)
90 {
91 while (this.events.ContainsKey(When))
92 When = When.AddTicks(this.gen.Next(1, 10));
93
94 this.events[When] = new ScheduledEvent(When, Callback, State);
95 this.RecalcTimerLocked();
96 }
97
98 return When;
99 }
100
101 private void RecalcTimerLocked()
102 {
103 this.timer?.Dispose();
104 this.timer = null;
105
106 LinkedList<DateTime> ToRemove = null;
107 DateTime Now = DateTime.Now;
108 TimeSpan TimeLeft;
109
110 foreach (KeyValuePair<DateTime, ScheduledEvent> Event in this.events)
111 {
112 TimeLeft = Event.Key - Now;
113 if (TimeLeft <= TimeSpan.Zero)
114 {
115 if (ToRemove is null)
116 ToRemove = new LinkedList<DateTime>();
117
118 ToRemove.AddLast(Event.Key);
119
120 Task.Run(Event.Value.Execute);
121 }
122 else
123 {
124 if (TimeLeft < OneDay)
125 this.timer = new Timer(this.TimerElapsed, null, TimeLeft, OnlyOnce);
126 else
127 this.timer = new Timer(this.TimerElapsed, null, OneDay, OnlyOnce);
128
129 break;
130 }
131 }
132
133 if (!(ToRemove is null))
134 {
135 foreach (DateTime TP in ToRemove)
136 this.events.Remove(TP);
137 }
138 }
139
140 private void TimerElapsed(object P)
141 {
142 try
143 {
144 lock (this.events)
145 {
146 this.RecalcTimerLocked();
147 }
148 }
149 catch (Exception ex)
150 {
151 Log.Exception(ex);
152 }
153 }
154
158 public void Clear()
159 {
160 try
161 {
162 lock (this.events)
163 {
164 this.events.Clear();
165 this.RecalcTimerLocked();
166 }
167 }
168 catch (Exception ex)
169 {
170 Log.Exception(ex);
171 }
172 }
173
182 public bool Remove(DateTime When)
183 {
184 lock (this.events)
185 {
186 if (this.events.Remove(When))
187 {
188 this.RecalcTimerLocked();
189 return true;
190 }
191 }
192
193 return false;
194 }
195
196 }
197}
Class representing an event.
Definition: Event.cs:10
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
Definition: Scheduler.cs:26
bool Remove(DateTime When)
Removes an event scheduled for a given point in time.
Definition: Scheduler.cs:182
Scheduler()
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
Definition: Scheduler.cs:39
DateTime Add(DateTime When, ScheduledEventCallbackAsync Callback, object State)
Adds an event.
Definition: Scheduler.cs:87
void Dispose()
IDisposable.Dispose
Definition: Scheduler.cs:46
DateTime Add(DateTime When, ScheduledEventCallback Callback, object State)
Adds an event.
Definition: Scheduler.cs:66
void Clear()
Clears all pending events.
Definition: Scheduler.cs:158
delegate void ScheduledEventCallback(object State)
Callback method for scheduled events.
delegate Task ScheduledEventCallbackAsync(object State)
Callback method for asynchronous scheduled events.