Skip to content

Instantly share code, notes, and snippets.

@programmation
Created September 23, 2015 23:01
Show Gist options
  • Save programmation/7d0fbd272165c07d1603 to your computer and use it in GitHub Desktop.
Save programmation/7d0fbd272165c07d1603 to your computer and use it in GitHub Desktop.
Async BeginInvokeOnMainThread returning a result
// from https://forums.xamarin.com/discussion/comment/154636/#Comment_154636
public static Task<T> BeginInvokeOnMainThreadAsync<T>(Func<T> a)
{
var tcs = new TaskCompletionSource<T>();
Device.BeginInvokeOnMainThread(() =>
{
try
{
var result = a();
tcs.SetResult(result);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
return tcs.Task;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment