-
Notifications
You must be signed in to change notification settings - Fork 294
/
UiDispatcher.cs
118 lines (102 loc) · 4.4 KB
/
UiDispatcher.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Threading;
using System.Threading.Tasks;
using Rubberduck.VBEditor.Utility;
namespace Rubberduck.Parsing.UIContext
{
public class UiDispatcher : IUiDispatcher
{
// thanks to Pellared on http://stackoverflow.com/a/12909070/1188513
private readonly IUiContextProvider _contextProvider;
public UiDispatcher(IUiContextProvider contextProvider)
{
_contextProvider = contextProvider;
}
/// <summary>
/// Invokes an action asynchronously on the UI thread.
/// </summary>
/// <param name="action">The action that must be executed.</param>
public void InvokeAsync(Action action)
{
CheckInitialization();
_contextProvider.UiContext.Post(x => action(), null);
}
/// <summary>
/// Executes an action on the UI thread. If this method is called
/// from the UI thread, the action is executed immendiately. If the
/// method is called from another thread, the action will be enqueued
/// on the UI thread's dispatcher and executed asynchronously.
/// <para>For additional operations on the UI thread, you can get a
/// reference to the UI thread's context thanks to the property
/// <see cref="UiContext" /></para>.
/// </summary>
/// <param name="action">The action that will be executed on the UI
/// thread</param>
public void Invoke(Action action)
{
CheckInitialization();
if (_contextProvider.UiContext == SynchronizationContext.Current)
{
action();
}
else
{
InvokeAsync(action);
}
}
/// <summary>
/// Starts a task on the ui thread.
/// </summary>
/// <param name="action">The action that will be executed on the UI
/// thread.</param>
/// <param name="token">Optional cancellation token</param>
/// <param name="options">Optional TaskCreationOptions</param>
/// <returns></returns>
public Task StartTask(Action action, CancellationToken token, TaskCreationOptions options = TaskCreationOptions.None)
{
CheckInitialization();
return Task.Factory.StartNew(action, token, options, _contextProvider.UiTaskScheduler);
}
//This separate overload is necessary because CancellationToken.None is not a compile-time constant and thus cannot be used as default value.
public Task StartTask(Action action, TaskCreationOptions options = TaskCreationOptions.None)
{
return StartTask(action, CancellationToken.None, options);
}
/// <summary>
/// Starts a task returning a value on the ui thread.
/// </summary>
/// <param name="func">The function that will be executed on the UI
/// thread.</param>
/// <param name="token">Optional cancellation token</param>
/// <param name="options">Optional TaskCreationOptions</param>
/// <returns></returns>
public Task<T> StartTask<T>(Func<T> func, CancellationToken token, TaskCreationOptions options = TaskCreationOptions.None)
{
CheckInitialization();
return Task.Factory.StartNew(func, token, options, _contextProvider.UiTaskScheduler);
}
//This separate overload is necessary because CancellationToken.None is not a compile-time constant and thus cannot be used as default value.
public Task<T> StartTask<T>(Func<T> func, TaskCreationOptions options = TaskCreationOptions.None)
{
return StartTask(func, CancellationToken.None, options);
}
/// <remarks>
/// Depends on the static method: <see cref="UiContextProvider.Initialize"/>
/// </remarks>
private void CheckInitialization()
{
if (_contextProvider.UiContext == null)
{
throw new InvalidOperationException("UiContext is not initialized. Invoke UiContextProvider.Initialize() from the UI thread first.");
}
}
public static void Shutdown()
{
//Invoke(() =>
//{
// LogManager.GetCurrentClassLogger().Debug("Invoking shutdown on UI thread dispatcher.");
// Dispatcher.CurrentDispatcher.InvokeShutdown();
//});
}
}
}