Created
July 23, 2018 09:22
-
-
Save mmierzwa/aa5587ab4c3d2dbc4208acb4abf74763 to your computer and use it in GitHub Desktop.
Example usage of `TaskCompletionSource`
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
public class DeviceThreadDispatcher : IThreadDispatcher | |
{ | |
private static int _uiThreadId; | |
public bool IsOnUiThread => Environment.CurrentManagedThreadId == _uiThreadId; | |
public static void Initialize(int uiThreadId) => _uiThreadId = uiThreadId; | |
public T RequestMainThreadAsyncOperation<T>(Func<Task<T>> operation) | |
{ | |
var result = default(T); | |
var taskCompletionSource = new TaskCompletionSource<bool>(); | |
Device.BeginInvokeOnMainThread(async () => | |
{ | |
result = await operation(); | |
taskCompletionSource.SetResult(true); | |
}); | |
if (taskCompletionSource.Task.Result) | |
{ | |
return result; | |
} | |
return default (T); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment