private static InvokeAsync ( this dispatcher, System.Action action ) : System.Threading.Tasks.Task | ||
dispatcher | this | The dispatcher. |
action | System.Action | The action. |
return | System.Threading.Tasks.Task |
private static Task InvokeAsync(this Dispatcher dispatcher, Action action)
{
var tcs = new TaskCompletionSource<bool>();
var dispatcherOperation = dispatcher.BeginInvoke(new Action(() =>
{
try
{
action();
}
catch (Exception ex)
{
tcs.SetException(ex);
}
}), null);
dispatcherOperation.Completed += (sender, e) => tcs.SetResult(true);
dispatcherOperation.Aborted += (sender, e) => tcs.SetCanceled();
return tcs.Task;
}
#endif