Skip to content

Instantly share code, notes, and snippets.

@mmierzwa
Created July 23, 2018 09:22
Show Gist options
  • Save mmierzwa/aa5587ab4c3d2dbc4208acb4abf74763 to your computer and use it in GitHub Desktop.
Save mmierzwa/aa5587ab4c3d2dbc4208acb4abf74763 to your computer and use it in GitHub Desktop.
Example usage of `TaskCompletionSource`
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