TaskAwaitable.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. using System.Runtime.CompilerServices;
  6. namespace System.Threading.Tasks
  7. {
  8. public sealed class TaskAwaitable : IAwaitable, IAwaiter
  9. {
  10. private readonly ConfiguredTaskAwaitable.ConfiguredTaskAwaiter _task;
  11. private readonly IAsyncScheduler _scheduler;
  12. private readonly CancellationToken _token;
  13. public TaskAwaitable(Task task, bool continueOnCapturedContext, IAsyncScheduler scheduler, CancellationToken token)
  14. {
  15. if (task == null)
  16. throw new ArgumentNullException(nameof(task));
  17. _task = task.ConfigureAwait(continueOnCapturedContext).GetAwaiter();
  18. _scheduler = scheduler;
  19. _token = token;
  20. }
  21. public bool IsCompleted => _task.IsCompleted;
  22. public IAwaiter GetAwaiter() => this;
  23. public void GetResult()
  24. {
  25. _token.ThrowIfCancellationRequested();
  26. _task.GetResult();
  27. }
  28. public void OnCompleted(Action continuation)
  29. {
  30. var cancel = default(IDisposable);
  31. if (_token.CanBeCanceled)
  32. {
  33. cancel = _token.Register(() =>
  34. {
  35. Interlocked.Exchange(ref continuation, null)?.Invoke();
  36. });
  37. }
  38. try
  39. {
  40. _task.OnCompleted(() =>
  41. {
  42. void Invoke()
  43. {
  44. cancel?.Dispose();
  45. Interlocked.Exchange(ref continuation, null)?.Invoke();
  46. }
  47. if (_scheduler != null)
  48. {
  49. var t = _scheduler.ExecuteAsync(ct =>
  50. {
  51. Invoke();
  52. return Task.CompletedTask;
  53. }, _token);
  54. }
  55. else
  56. {
  57. Invoke();
  58. }
  59. });
  60. }
  61. catch
  62. {
  63. cancel?.Dispose();
  64. throw;
  65. }
  66. }
  67. }
  68. public sealed class TaskAwaitable<T> : IAwaitable<T>, IAwaiter<T>
  69. {
  70. private readonly ConfiguredTaskAwaitable<T>.ConfiguredTaskAwaiter _task;
  71. private readonly IAsyncScheduler _scheduler;
  72. private readonly CancellationToken _token;
  73. public TaskAwaitable(Task<T> task, bool continueOnCapturedContext, IAsyncScheduler scheduler, CancellationToken token)
  74. {
  75. if (task == null)
  76. throw new ArgumentNullException(nameof(task));
  77. _task = task.ConfigureAwait(continueOnCapturedContext).GetAwaiter();
  78. _scheduler = scheduler;
  79. _token = token;
  80. }
  81. public bool IsCompleted => _task.IsCompleted;
  82. public IAwaiter<T> GetAwaiter() => this;
  83. public T GetResult()
  84. {
  85. _token.ThrowIfCancellationRequested();
  86. return _task.GetResult();
  87. }
  88. public void OnCompleted(Action continuation)
  89. {
  90. var cancel = default(IDisposable);
  91. if (_token.CanBeCanceled)
  92. {
  93. cancel = _token.Register(() =>
  94. {
  95. Interlocked.Exchange(ref continuation, null)?.Invoke();
  96. });
  97. }
  98. try
  99. {
  100. _task.OnCompleted(() =>
  101. {
  102. void Invoke()
  103. {
  104. cancel?.Dispose();
  105. Interlocked.Exchange(ref continuation, null)?.Invoke();
  106. }
  107. if (_scheduler != null)
  108. {
  109. var t = _scheduler.ExecuteAsync(ct =>
  110. {
  111. Invoke();
  112. return Task.CompletedTask;
  113. }, _token);
  114. }
  115. else
  116. {
  117. Invoke();
  118. }
  119. });
  120. }
  121. catch
  122. {
  123. cancel?.Dispose();
  124. throw;
  125. }
  126. }
  127. }
  128. }