-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathScheduledEvent.cs
More file actions
93 lines (83 loc) · 2.24 KB
/
ScheduledEvent.cs
File metadata and controls
93 lines (83 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Threading.Tasks;
using Waher.Events;
namespace Waher.Runtime.Timing
{
/// <summary>
/// Contains information about a scheduled event.
/// </summary>
internal class ScheduledEvent
{
private readonly DateTime when;
private readonly Action<object> eventMethod;
private readonly object state;
/// <summary>
/// Contains information about a scheduled event.
/// </summary>
/// <param name="When">When an event is to be executed.</param>
/// <param name="EventMethod">Method to call when event is executed.</param>
/// <param name="State">State object passed on to <paramref name="EventMethod"/>.</param>
public ScheduledEvent(DateTime When, Action<object> EventMethod, object State)
{
this.when = When;
this.eventMethod = EventMethod;
this.state = State;
}
/// <summary>
/// Contains information about a scheduled event.
/// </summary>
/// <param name="When">When an event is to be executed.</param>
/// <param name="EventMethod">Method to call when event is executed.</param>
/// <param name="State">State object passed on to <paramref name="EventMethod"/>.</param>
public ScheduledEvent(DateTime When, Func<object, Task> EventMethod, object State)
{
this.when = When;
this.eventMethod = this.AsyncCallback;
this.state = new object[] { EventMethod, State };
}
private async void AsyncCallback(object State)
{
try
{
object[] P = (object[])State;
Func<object, Task> Callback = (Func<object, Task>)P[0];
State = P[1];
if (!(Callback is null))
await Callback(State);
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// When an event is to be executed.
/// </summary>
public DateTime When => this.when;
/// <summary>
/// Method to call when event is executed.
/// </summary>
public Action<object> EventMethod => this.eventMethod;
/// <summary>
/// State object passed on to <see cref="EventMethod"/>.
/// </summary>
public object State => this.state;
/// <summary>
/// Executes the event.
/// </summary>
public void Execute()
{
if (!(this.eventMethod is null))
{
try
{
this.eventMethod(this.state);
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
}
}
}