Skip to content

Commit 5b153af

Browse files
authored
Merge pull request #618 from lukasf/dev
Fix DispatcherHelper open issues
2 parents 4b4608d + c576b39 commit 5b153af

File tree

2 files changed

+67
-49
lines changed

2 files changed

+67
-49
lines changed

Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static Task<T> ExecuteOnUIThreadAsync<T>(Func<Task<T>> function, CoreDisp
4242
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
4343
/// <param name="priority">Dispatcher execution priority, default is normal</param>
4444
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
45-
public static Task<T> ExecuteOnUIThreadAsync<T>(CoreApplicationView viewToExecuteOn, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
45+
public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToExecuteOn, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
4646
{
4747
if (viewToExecuteOn == null)
4848
{
@@ -59,15 +59,14 @@ public static Task<T> ExecuteOnUIThreadAsync<T>(CoreApplicationView viewToExecut
5959
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
6060
/// <param name="priority">Dispatcher execution priority, default is normal</param>
6161
/// <returns>Awaitable Task</returns>
62-
public static Task ExecuteOnUIThreadAsync(CoreApplicationView viewToExecuteOn, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
62+
public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
6363
{
64-
return ExecuteOnUIThreadAsync<object>(
65-
viewToExecuteOn,
66-
async () =>
67-
{
68-
await function().ConfigureAwait(false);
69-
return null;
70-
}, priority);
64+
if (viewToExecuteOn == null)
65+
{
66+
throw new ArgumentNullException("viewToExecuteOn can't be null!");
67+
}
68+
69+
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
7170
}
7271

7372
/// <summary>
@@ -78,12 +77,7 @@ public static Task ExecuteOnUIThreadAsync(CoreApplicationView viewToExecuteOn, F
7877
/// <returns>Awaitable Task</returns>
7978
public static Task ExecuteOnUIThreadAsync(Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
8079
{
81-
return ExecuteOnUIThreadAsync<object>(
82-
async () =>
83-
{
84-
await function().ConfigureAwait(false);
85-
return null;
86-
}, priority);
80+
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
8781
}
8882

8983
/// <summary>
@@ -93,7 +87,7 @@ public static Task ExecuteOnUIThreadAsync(Func<Task> function, CoreDispatcherPri
9387
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread</param>
9488
/// <param name="priority">Dispatcher execution priority, default is normal</param>
9589
/// <returns>Awaitable Task/></returns>
96-
public static Task ExecuteOnUIThreadAsync(CoreApplicationView viewToExecuteOn, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
90+
public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
9791
{
9892
if (viewToExecuteOn == null)
9993
{
@@ -122,7 +116,7 @@ public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriorit
122116
/// <param name="function">Synchronous function with return type <typeparamref name="T"/> to be executed on UI thread</param>
123117
/// <param name="priority">Dispatcher execution priority, default is normal</param>
124118
/// <returns>Awaitable Task with type <typeparamref name="T"/></returns>
125-
public static Task<T> ExecuteOnUIThreadAsync<T>(CoreApplicationView viewToExecuteOn, Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
119+
public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToExecuteOn, Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
126120
{
127121
if (viewToExecuteOn == null)
128122
{
@@ -159,28 +153,28 @@ public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<
159153
throw new ArgumentNullException("function can't be null!");
160154
}
161155

162-
TaskCompletionSource<T> taskCompletionSource = new TaskCompletionSource<T>();
156+
var taskCompletionSource = new TaskCompletionSource<T>();
163157

164158
var ignored = dispatcher.RunAsync(priority, async () =>
165-
{
166-
try
167-
{
168-
var awaitableResult = function();
169-
if (awaitableResult != null)
170-
{
171-
var result = await awaitableResult.ConfigureAwait(false);
172-
taskCompletionSource.SetResult(result);
173-
}
174-
else
175-
{
176-
taskCompletionSource.SetResult(default(T));
177-
}
178-
}
179-
catch (Exception e)
180-
{
181-
taskCompletionSource.SetException(e);
182-
}
183-
});
159+
{
160+
try
161+
{
162+
var awaitableResult = function();
163+
if (awaitableResult != null)
164+
{
165+
var result = await awaitableResult.ConfigureAwait(false);
166+
taskCompletionSource.SetResult(result);
167+
}
168+
else
169+
{
170+
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
171+
}
172+
}
173+
catch (Exception e)
174+
{
175+
taskCompletionSource.SetException(e);
176+
}
177+
});
184178

185179
return taskCompletionSource.Task;
186180
}
@@ -194,12 +188,35 @@ public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<
194188
/// <returns>Awaitable Task</returns>
195189
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
196190
{
197-
return dispatcher.AwaitableRunAsync<object>(
198-
async () =>
191+
if (function == null)
199192
{
200-
await function().ConfigureAwait(false);
201-
return null;
202-
}, priority);
193+
throw new ArgumentNullException("function can't be null!");
194+
}
195+
196+
var taskCompletionSource = new TaskCompletionSource<object>();
197+
198+
var ignored = dispatcher.RunAsync(priority, async () =>
199+
{
200+
try
201+
{
202+
var awaitableResult = function();
203+
if (awaitableResult != null)
204+
{
205+
await awaitableResult.ConfigureAwait(false);
206+
taskCompletionSource.SetResult(null);
207+
}
208+
else
209+
{
210+
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
211+
}
212+
}
213+
catch (Exception e)
214+
{
215+
taskCompletionSource.SetException(e);
216+
}
217+
});
218+
219+
return taskCompletionSource.Task;
203220
}
204221

205222
/// <summary>
@@ -217,7 +234,7 @@ public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<
217234
throw new ArgumentNullException("function can't be null!");
218235
}
219236

220-
TaskCompletionSource<T> taskCompletionSource = new TaskCompletionSource<T>();
237+
var taskCompletionSource = new TaskCompletionSource<T>();
221238

222239
var ignored = dispatcher.RunAsync(priority, () =>
223240
{
@@ -243,11 +260,11 @@ public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<
243260
/// <returns>Awaitable Task</returns>
244261
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
245262
{
246-
return dispatcher.AwaitableRunAsync<object>(
263+
return dispatcher.AwaitableRunAsync(
247264
() =>
248265
{
249266
function();
250-
return null;
267+
return (object)null;
251268
}, priority);
252269
}
253270
}

docs/helpers/DispatcherHelper.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# DispatcherHelper
22

3-
The DispatcherHelper class enables easy interaction with [CoreDispatcher](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.aspx), mainly in the case of executing a block of code in UI thread from a non-UI thread.
3+
The DispatcherHelper class enables easy interaction with [CoreDispatcher](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.aspx), mainly in the case of executing a block of code on the UI thread from a non-UI thread.
44

55
_What is included in the helper?_
66
- Extension method with overloads for [CoreDispatcher](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.aspx) class.
7-
- Static helper methods of executing a specific function in UI thread current application's main window (configu)
7+
- Extension method with overloads for [CoreApplicationView](https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.core.coreapplicationview.aspx) (for multi window applications).
8+
- Static helper methods for executing a specific function on the UI thread of the current application's main window.
89

910

1011
## Example
@@ -14,7 +15,7 @@ _What is included in the helper?_
1415
// Executing from a non-UI thread with helper method
1516
int returnedFromUIThread = await DispatcherHelper.ExecuteOnUIThreadAsync<int>(() =>
1617
{
17-
// Code to execute with UI thread
18+
// Code to execute on main window's UI thread
1819
NormalTextBlock.Text = "Updated from a random thread!";
1920
return 1;
2021
});
@@ -39,5 +40,5 @@ _What is included in the helper?_
3940

4041
## API
4142

42-
* [Connection Helper source code](https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs)
43+
* [DispatcherHelper source code](https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp/Helpers/DispatcherHelper.cs)
4344

0 commit comments

Comments
 (0)