SHARE
TWEET

AsyncOperationExtensions.cs

a guest Aug 14th, 2016 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5.  
  6. public static class AsyncOperationExtensions
  7. {
  8.     public static AsyncTools.Awaiter GetAwaiter(this AsyncOperation asyncOp) => new AsyncOperationAwaiter(asyncOp);
  9.  
  10.     private class AsyncOperationAwaiter : AsyncTools.Awaiter
  11.     {
  12.         private readonly AsyncOperation asyncOp;
  13.  
  14.         public AsyncOperationAwaiter(AsyncOperation asyncOp)
  15.         {
  16.             this.asyncOp = asyncOp;
  17.         }
  18.  
  19.         public override bool IsCompleted => asyncOp.isDone;
  20.  
  21.         public override void OnCompleted(Action action)
  22.         {
  23.             Task.Factory.StartNew(async () =>
  24.                                         {
  25.                                             while (asyncOp.isDone == false)
  26.                                             {
  27.                                                 await 0;
  28.                                             }
  29.                                             action();
  30.                                         },
  31.                 CancellationToken.None,
  32.                 TaskCreationOptions.None,
  33.                 UnityScheduler.UpdateScheduler);
  34.         }
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top