Advertisement
Guest User

AsyncOperationExtensions.cs

a guest
Aug 14th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement