AsyncAwaitable.cs 759 B

12345678910111213141516171819202122232425262728
  1. namespace XGame.Framework.Asyncs
  2. {
  3. public abstract class AsyncAwaitable<TAwaiter> : Async, IAsyncAwaitable<TAwaiter> where TAwaiter : IAwaiter, new()
  4. {
  5. protected TAwaiter awaiter;
  6. public TAwaiter GetAwaiter()
  7. {
  8. if (awaiter == null)
  9. {
  10. awaiter = new TAwaiter();
  11. if (IsCompleted)
  12. {
  13. awaiter.Completed(this);
  14. }
  15. }
  16. return awaiter;
  17. }
  18. public override void Completed()
  19. {
  20. //先自己的回调处理完,再处理awaiter
  21. base.Completed();
  22. awaiter?.Completed(this);
  23. }
  24. }
  25. public class AsyncAwaitable : AsyncAwaitable<Awaiter> { }
  26. }