-
Notifications
You must be signed in to change notification settings - Fork 47
/
Dispatcher.cs
63 lines (55 loc) · 1.93 KB
/
Dispatcher.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
using System;
using System.Threading.Tasks;
namespace TcOpen.Inxton.Threading
{
/// <summary>
/// Provides access to UI dispatcher of currently running application.
/// </summary>
public class Dispatcher : IDispatcher
{
private Dispatcher()
{
}
private static IDispatcher _dispatcher;
/// <summary>
/// Sets the dispatcher for running application.
/// </summary>
/// <param name="dispatcher"></param>
public static void SetDispatcher(IDispatcher dispatcher)
{
_dispatcher = dispatcher;
}
/// <summary>
/// Invokes an action on the UI dispatcher of the currently running application.
/// The appropriate dispatcher must be set at the start of the application. To set dispatcher use <see cref="SetDispatcher(IDispatcher)"/> method.
/// </summary>
/// <param name="action">Action to run.</param>
public void Invoke(Action action)
{
_dispatcher?.Invoke(action);
}
// <summary>
/// Invokes an action on the UI dispatcher of the currently running application asynchronously
/// The appropriate dispatcher must be set at the start of the application. To set dispatcher use <see cref="SetDispatcher(IDispatcher)"/> method.
/// </summary>
/// <param name="action">Action to run.</param>
public Task InvokeAsync(Action action)
{
return _dispatcher?.InvokeAsync(action);
}
private volatile static object mutex = new object();
/// <summary>
/// Gets dispatcher mediator of currently running application.
/// </summary>
public static IDispatcher Get
{
get
{
lock (mutex)
{
return _dispatcher ?? new Dispatcher();
}
}
}
}
}