Guest User

Untitled

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. public class DeviceThreadDispatcher : IThreadDispatcher
  2. {
  3. private static int _uiThreadId;
  4.  
  5. public bool IsOnUiThread => Environment.CurrentManagedThreadId == _uiThreadId;
  6.  
  7. public static void Initialize(int uiThreadId) => _uiThreadId = uiThreadId;
  8.  
  9. public T RequestMainThreadAsyncOperation<T>(Func<Task<T>> operation)
  10. {
  11. var result = default(T);
  12. var taskCompletionSource = new TaskCompletionSource<bool>();
  13.  
  14. Device.BeginInvokeOnMainThread(async () =>
  15. {
  16. result = await operation();
  17. taskCompletionSource.SetResult(true);
  18. });
  19.  
  20. if (taskCompletionSource.Task.Result)
  21. {
  22. return result;
  23. }
  24.  
  25. return default (T);
  26. }
  27. }
Add Comment
Please, Sign In to add comment