-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextEventBase.cs
68 lines (62 loc) · 2.34 KB
/
ContextEventBase.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SynchronizedEvents
{
public abstract class ContextEventBase<THandler, TArgs>
{
private readonly List<(SynchronizationContext context, THandler handler)> _handlers = new List<(SynchronizationContext context, THandler handler)>(0);
public void Add(THandler handler)
{
if (handler == null) return;
lock (_handlers) _handlers.Add((SynchronizationContext.Current, handler));
}
public void Remove(THandler handler)
{
if (handler == null) return;
lock (_handlers)
{
var i = 0;
foreach (var item in _handlers)
{
if (item.handler.Equals(handler)) { _handlers.RemoveAt(i); break; }
i++;
}
}
}
public async Task Invoke(object sender, TArgs args)
{
(SynchronizationContext context, THandler handler)[] handlers;
lock (_handlers) handlers = _handlers.ToArray();
var tasks = handlers
.GroupBy(x => x.context, x => x.handler)
.Select(g => invokeContext(g.Key, g))
.ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
async Task invokeContext(SynchronizationContext context, IEnumerable<THandler> l)
{
if (context != null)
{
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
context.Post(o =>
{
try { invokeHandlers(l); tcs.TrySetResult(true); }
catch (Exception e) { tcs.TrySetException(e); }
}, null);
await tcs.Task.ConfigureAwait(false);
}
else
{
await Task.Run(() => invokeHandlers(l)).ConfigureAwait(false);
}
}
void invokeHandlers(IEnumerable<THandler> l)
{
foreach (var h in l) Invoke(h, sender, args);
}
}
protected abstract void Invoke(THandler handler, object sender, TArgs args);
}
}