-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround for Dispatcher.
RunIdleAsync
- Loading branch information
1 parent
6cfefb2
commit 07ca2a5
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
ShortDev.Uwp.FullTrust/Windows/UI/Core/CoreDispatcherExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Windows.UI.Core | ||
{ | ||
public static class CoreDispatcherExtensions | ||
{ | ||
// https://github.com/Microsoft/Windows-task-snippets/blob/master/tasks/UI-thread-task-await-from-background-thread.md | ||
|
||
public static async Task<T> RunTaskAsync<T>(this CoreDispatcher @this, Func<Task<T>> callback, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) | ||
{ | ||
TaskCompletionSource<T> taskCompletionSource = new(); | ||
_ = @this.RunAsync(priority, async () => | ||
{ | ||
try | ||
{ | ||
taskCompletionSource.SetResult(await callback()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
taskCompletionSource.SetException(ex); | ||
} | ||
}); | ||
return await taskCompletionSource.Task; | ||
} | ||
|
||
public static async Task RunTaskAsync(this CoreDispatcher @this, Func<Task> callback, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) | ||
=> await RunTaskAsync( | ||
@this, | ||
async () => | ||
{ | ||
await callback(); | ||
return true; | ||
}, | ||
priority | ||
); | ||
|
||
public static async Task RunTaskAsync(this CoreDispatcher @this, Action callback, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) | ||
=> await RunTaskAsync( | ||
@this, | ||
() => | ||
{ | ||
callback(); | ||
return Task.FromResult(true); | ||
}, | ||
priority | ||
); | ||
} | ||
} |