ForEach.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static void ForEach<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException("source");
  17. if (action == null)
  18. throw new ArgumentNullException("action");
  19. source.ForEachAsync(action)
  20. .Wait();
  21. }
  22. public static void ForEach<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action)
  23. {
  24. if (source == null)
  25. throw new ArgumentNullException("source");
  26. if (action == null)
  27. throw new ArgumentNullException("action");
  28. source.ForEachAsync(action)
  29. .Wait();
  30. }
  31. public static void ForEach<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (action == null)
  36. throw new ArgumentNullException(nameof(action));
  37. source.ForEachAsync(action, cancellationToken)
  38. .Wait(cancellationToken);
  39. }
  40. public static void ForEach<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  41. {
  42. if (source == null)
  43. throw new ArgumentNullException(nameof(source));
  44. if (action == null)
  45. throw new ArgumentNullException(nameof(action));
  46. source.ForEachAsync(action, cancellationToken)
  47. .Wait(cancellationToken);
  48. }
  49. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException("source");
  53. if (action == null)
  54. throw new ArgumentNullException("action");
  55. return ForEachAsync(source, action, CancellationToken.None);
  56. }
  57. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException("source");
  61. if (action == null)
  62. throw new ArgumentNullException("action");
  63. return ForEachAsync(source, action, CancellationToken.None);
  64. }
  65. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  66. {
  67. if (source == null)
  68. throw new ArgumentNullException(nameof(source));
  69. if (action == null)
  70. throw new ArgumentNullException(nameof(action));
  71. return source.ForEachAsync((x, i) => action(x), cancellationToken);
  72. }
  73. public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (action == null)
  78. throw new ArgumentNullException(nameof(action));
  79. return ForEachAsync_(source, action, cancellationToken);
  80. }
  81. private static async Task ForEachAsync_<TSource>(IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken)
  82. {
  83. var index = 0;
  84. using (var e = source.GetEnumerator())
  85. {
  86. while (await e.MoveNext(cancellationToken)
  87. .ConfigureAwait(false))
  88. {
  89. action(e.Current, checked(index++));
  90. }
  91. }
  92. }
  93. }
  94. }