| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Microsoft.Azure.Batch |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Provides a set of static (Shared in Visual Basic) methods for working with sequences that |
| | 13 | | /// implement <see cref="IPagedEnumerable{T}"/>. |
| | 14 | | /// </summary> |
| | 15 | | public static class PagedEnumerableExtensions |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Iterates over an <see cref="IPagedEnumerable{T}"/> sequence, invoking an asynchronous delegate for each elem |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="source">The <see cref="IPagedEnumerable{T}"/> to iterate over.</param> |
| | 21 | | /// <param name="body">The asynchronous delegate to execute for each element in <paramref name="source"/>.</para |
| | 22 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 23 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the iteration operation. The task |
| | 24 | | /// completes when iteration is complete.</returns> |
| | 25 | | /// <remarks>This method processes elements sequentially, not concurrently. That is, for each element in the |
| | 26 | | /// sequence, the method awaits the asynchronous delegate before processing the next element.</remarks> |
| | 27 | | public static async Task ForEachAsync<T>(this IPagedEnumerable<T> source, Func<T, Task> body, CancellationToken |
| | 28 | | { |
| 4 | 29 | | if (source == null) |
| | 30 | | { |
| 1 | 31 | | throw new ArgumentNullException("source") ; |
| | 32 | | } |
| 3 | 33 | | if (body == null) |
| | 34 | | { |
| 1 | 35 | | throw new ArgumentNullException("body"); |
| | 36 | | } |
| | 37 | |
|
| 28 | 38 | | await source.ForEachAsync((t, ct) => body(t), cancellationToken).ConfigureAwait(false); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Iterates over an <see cref="IPagedEnumerable{T}"/> sequence, invoking an asynchronous delegate for each elem |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="source">The <see cref="IPagedEnumerable{T}"/> to iterate over.</param> |
| | 45 | | /// <param name="body">The asynchronous delegate to execute for each element in <paramref name="source"/>.</para |
| | 46 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 47 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the iteration operation. The task |
| | 48 | | /// completes when iteration is complete.</returns> |
| | 49 | | /// <remarks>This method processes elements sequentially, not concurrently. That is, for each element in the |
| | 50 | | /// sequence, the method awaits the asynchronous delegate before processing the next element.</remarks> |
| | 51 | | public static async Task ForEachAsync<T>(this IPagedEnumerable<T> source, Func<T, CancellationToken, Task> body, |
| | 52 | | { |
| 2 | 53 | | if (source == null) |
| | 54 | | { |
| 0 | 55 | | throw new ArgumentNullException("source"); |
| | 56 | | } |
| 2 | 57 | | if (body == null) |
| | 58 | | { |
| 0 | 59 | | throw new ArgumentNullException("body"); |
| | 60 | | } |
| | 61 | |
|
| 2 | 62 | | using (IPagedEnumerator<T> enumerator = source.GetPagedEnumerator()) |
| | 63 | | { |
| 28 | 64 | | while (await enumerator.MoveNextAsync(cancellationToken).ConfigureAwait(false)) |
| | 65 | | { |
| 27 | 66 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 67 | |
|
| 26 | 68 | | await body(enumerator.Current, cancellationToken).ConfigureAwait(false); |
| | 69 | | } |
| 1 | 70 | | } |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Iterates over an <see cref="IPagedEnumerable{T}"/> sequence, invoking a synchronous delegate for each elemen |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="source">The <see cref="IPagedEnumerable{T}"/> to iterate over.</param> |
| | 77 | | /// <param name="body">The delegate to execute for each element in <paramref name="source"/>.</param> |
| | 78 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 79 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the iteration operation. The task |
| | 80 | | /// completes when iteration is complete.</returns> |
| | 81 | | /// <remarks>This method processes elements sequentially, not concurrently. That is, for each element in the |
| | 82 | | /// sequence, the method completes execution of the delegate before processing the next element.</remarks> |
| | 83 | | public static async Task ForEachAsync<T>(this IPagedEnumerable<T> source, Action<T> body, CancellationToken canc |
| | 84 | | { |
| 9 | 85 | | if (source == null) |
| | 86 | | { |
| 1 | 87 | | throw new ArgumentNullException("source"); |
| | 88 | | } |
| 8 | 89 | | if (body == null) |
| | 90 | | { |
| 1 | 91 | | throw new ArgumentNullException("body"); |
| | 92 | | } |
| | 93 | |
|
| 7 | 94 | | using (IPagedEnumerator<T> enumerator = source.GetPagedEnumerator()) |
| | 95 | | { |
| 51 | 96 | | while (await enumerator.MoveNextAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false |
| | 97 | | { |
| 46 | 98 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 99 | |
|
| 44 | 100 | | body(enumerator.Current); |
| | 101 | | } |
| 5 | 102 | | } |
| 5 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Creates a <see cref="List{T}" /> from an <see cref="IPagedEnumerable{T}"/>. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="source">The <see cref="IPagedEnumerable{T}"/> to create a list from.</param> |
| | 109 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 110 | | /// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous operation. The result |
| | 111 | | /// of the task is a <see cref="List{T}" /> containing all elements of the source sequence.</returns> |
| | 112 | | public static async Task<List<T>> ToListAsync<T>(this IPagedEnumerable<T> source, CancellationToken cancellation |
| | 113 | | { |
| 6 | 114 | | if (source == null) |
| | 115 | | { |
| 1 | 116 | | throw new ArgumentNullException("source"); |
| | 117 | | } |
| | 118 | |
|
| 5 | 119 | | List<T> results = new List<T>(); |
| | 120 | |
|
| 23 | 121 | | await source.ForEachAsync(item => results.Add(item), cancellationToken).ConfigureAwait(continueOnCapturedCon |
| | 122 | |
|
| 4 | 123 | | return results; |
| 4 | 124 | | } |
| | 125 | | } |
| | 126 | | } |