Created
September 23, 2015 23:01
-
-
Save programmation/7d0fbd272165c07d1603 to your computer and use it in GitHub Desktop.
Async BeginInvokeOnMainThread returning a result
This file contains hidden or 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
// 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