forked from geloczi/synologydotnet-audiostation-wpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHandlerExtensions.cs
56 lines (52 loc) · 2.36 KB
/
EventHandlerExtensions.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
using System;
using System.Threading.Tasks;
namespace Utils
{
public static class EventHandlerExtensions
{
/// <summary>
/// Invokes all subscribed event handler methods.
/// The "Invoke" or "BeginInvoke" methods are not working with more than one subscribers, this helper is a solution for that.
/// </summary>
public static void Fire(this EventHandler eventHandler, object sender, EventArgs eventArgs)
{
if (eventHandler is null)
return;
foreach (EventHandler handler in eventHandler.GetInvocationList())
handler(sender, eventArgs);
}
/// <summary>
/// Invokes all subscribed event handler methods.
/// The "Invoke" or "BeginInvoke" methods are not working with more than one subscribers, this helper is a solution for that.
/// </summary>
public static void Fire<TEventArgs>(this EventHandler<TEventArgs> eventHandler, object sender, TEventArgs eventArgs)
{
if (eventHandler is null)
return;
foreach (EventHandler<TEventArgs> handler in eventHandler.GetInvocationList())
handler(sender, eventArgs);
}
/// <summary>
/// Invokes all subscribed event handler methods.
/// The "Invoke" or "BeginInvoke" methods are not working with more than one subscribers, this helper is a solution for that.
/// </summary>
public static void FireAsync(this EventHandler eventHandler, object sender, EventArgs eventArgs)
{
if (eventHandler is null)
return;
foreach (EventHandler handler in eventHandler.GetInvocationList())
Task.Run(() => handler(sender, eventArgs));
}
/// <summary>
/// Invokes all subscribed event handler methods.
/// The "Invoke" or "BeginInvoke" methods are not working with more than one subscribers, this helper is a solution for that.
/// </summary>
public static void FireAsync<TEventArgs>(this EventHandler<TEventArgs> eventHandler, object sender, TEventArgs eventArgs)
{
if (eventHandler is null)
return;
foreach (EventHandler<TEventArgs> handler in eventHandler.GetInvocationList())
Task.Run(() => handler(sender, eventArgs));
}
}
}