1
0

ForEach.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !HAS_AWAIT_FOREACH
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. // REVIEW: Once we have C# 8.0 language support, we may want to do away with these methods. An open question is how to
  13. // provide support for cancellation, which could be offered through WithCancellation on the source. If we still
  14. // want to keep these methods, they may be a candidate for System.Interactive.Async if we consider them to be
  15. // non-standard (i.e. IEnumerable<T> doesn't have a ForEach extension method either).
  16. /// <summary>
  17. /// Invokes an action for each element in the async-enumerable sequence, and returns a Task object that will get signaled when the sequence terminates.
  18. /// </summary>
  19. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  20. /// <param name="source">Source sequence.</param>
  21. /// <param name="action">Action to invoke for each element in the async-enumerable sequence.</param>
  22. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  23. /// <returns>Task that signals the termination of the sequence.</returns>
  24. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is null.</exception>
  25. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  26. [Obsolete("Use the language support for async foreach instead.")]
  27. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken = default)
  28. {
  29. if (source == null)
  30. throw Error.ArgumentNull(nameof(source));
  31. if (action == null)
  32. throw Error.ArgumentNull(nameof(action));
  33. return Core(source, action, cancellationToken);
  34. static async Task Core(IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  35. {
  36. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  37. {
  38. action(item);
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// Invokes an action for each element in the async-enumerable sequence, incorporating the element's index, and returns a Task object that will get signaled when the sequence terminates.
  44. /// </summary>
  45. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  46. /// <param name="source">Source sequence.</param>
  47. /// <param name="action">Action to invoke for each element in the async-enumerable sequence.</param>
  48. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  49. /// <returns>Task that signals the termination of the sequence.</returns>
  50. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is null.</exception>
  51. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  52. [Obsolete("Use the language support for async foreach instead.")]
  53. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken = default)
  54. {
  55. if (source == null)
  56. throw Error.ArgumentNull(nameof(source));
  57. if (action == null)
  58. throw Error.ArgumentNull(nameof(action));
  59. return Core(source, action, cancellationToken);
  60. static async Task Core(IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  61. {
  62. var index = 0;
  63. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  64. {
  65. action(item, checked(index++));
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Invokes and awaits an asynchronous action on each element in the source sequence, and returns a task that is signaled when the sequence terminates.
  71. /// </summary>
  72. /// <typeparam name="TSource">Type of elements in the sequence.</typeparam>
  73. /// <param name="source">Source sequence.</param>
  74. /// <param name="action">Asynchronous action to invoke and await for each element in the source sequence.</param>
  75. /// <param name="cancellationToken">Optional cancellation token for cancelling the sequence at any time.</param>
  76. /// <returns>Task that signals the termination of the sequence.</returns>
  77. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is <see langword="null"/>.</exception>
  78. [GenerateAsyncOverload]
  79. [Obsolete("Use the language support for async foreach instead.")]
  80. private static Task ForEachAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> action, CancellationToken cancellationToken = default)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (action == null)
  85. throw Error.ArgumentNull(nameof(action));
  86. return Core(source, action, cancellationToken);
  87. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, Task> action, CancellationToken cancellationToken)
  88. {
  89. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  90. {
  91. await action(item).ConfigureAwait(false);
  92. }
  93. }
  94. }
  95. #if !NO_DEEP_CANCELLATION
  96. [GenerateAsyncOverload]
  97. [Obsolete("Use the language support for async foreach instead.")]
  98. internal static Task ForEachAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  99. {
  100. if (source == null)
  101. throw Error.ArgumentNull(nameof(source));
  102. if (action == null)
  103. throw Error.ArgumentNull(nameof(action));
  104. return Core(source, action, cancellationToken);
  105. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> action, CancellationToken cancellationToken)
  106. {
  107. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  108. {
  109. await action(item, cancellationToken).ConfigureAwait(false);
  110. }
  111. }
  112. }
  113. #endif
  114. /// <summary>
  115. /// Invokes and awaits an asynchronous action on each element in the source sequence, incorporating the element's index, and returns a task that is signaled when the sequence terminates.
  116. /// </summary>
  117. /// <typeparam name="TSource">Type of elements in the sequence.</typeparam>
  118. /// <param name="source">Source sequence.</param>
  119. /// <param name="action">Asynchronous action to invoke and await for each element in the source sequence; the second parameter represents the index of the element.</param>
  120. /// <param name="cancellationToken">Optional cancellation token for cancelling the sequence at any time.</param>
  121. /// <returns>Task that signals the termination of the sequence.</returns>
  122. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="action"/> is <see langword="null"/>.</exception>
  123. [GenerateAsyncOverload]
  124. [Obsolete("Use the language support for async foreach instead.")]
  125. private static Task ForEachAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken = default)
  126. {
  127. if (source == null)
  128. throw Error.ArgumentNull(nameof(source));
  129. if (action == null)
  130. throw Error.ArgumentNull(nameof(action));
  131. return Core(source, action, cancellationToken);
  132. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, int, Task> action, CancellationToken cancellationToken)
  133. {
  134. var index = 0;
  135. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  136. {
  137. await action(item, checked(index++)).ConfigureAwait(false);
  138. }
  139. }
  140. }
  141. #if !NO_DEEP_CANCELLATION
  142. [GenerateAsyncOverload]
  143. [Obsolete("Use the language support for async foreach instead.")]
  144. internal static Task ForEachAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  145. {
  146. if (source == null)
  147. throw Error.ArgumentNull(nameof(source));
  148. if (action == null)
  149. throw Error.ArgumentNull(nameof(action));
  150. return Core(source, action, cancellationToken);
  151. static async Task Core(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, Task> action, CancellationToken cancellationToken)
  152. {
  153. var index = 0;
  154. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  155. {
  156. await action(item, checked(index++), cancellationToken).ConfigureAwait(false);
  157. }
  158. }
  159. }
  160. #endif
  161. }
  162. }
  163. #endif