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