Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions EnumerableAsyncProcessor.Example/AsyncEnumerableExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using EnumerableAsyncProcessor.Extensions;

namespace EnumerableAsyncProcessor.Example;

public static class AsyncEnumerableExample
{
public static async Task RunExamples()
{
Console.WriteLine("=== IAsyncEnumerable Parallel Processing Examples ===\n");

// Example 1: ForEachAsync with parallel processing
Console.WriteLine("Example 1: Processing async enumerable items in parallel");
var items = GenerateAsyncNumbers(10);

await items
.ForEachAsync(async number =>
{
await Task.Delay(100); // Simulate I/O work
Console.WriteLine($"Processed {number} on thread {Thread.CurrentThread.ManagedThreadId}");
})
.ProcessInParallel(3)
.ExecuteAsync(); // Process with max 3 concurrent tasks

Console.WriteLine("\nExample 2: SelectAsync with transformation");
var transformedItems = GenerateAsyncNumbers(5);

var results = await transformedItems
.SelectAsync(async number =>
{
await Task.Delay(50);
return number * 2;
})
.ProcessInParallel(2)
.ExecuteAsync()
.ToListAsync();

Console.WriteLine($"Transformed results: {string.Join(", ", results)}");

// Example 3: High concurrency for I/O-bound operations
Console.WriteLine("\nExample 3: High concurrency I/O operations");
var ioItems = GenerateAsyncNumbers(20);

await ioItems
.ForEachAsync(async number =>
{
await SimulateApiCall(number);
Console.WriteLine($"API call {number} completed");
})
.ProcessInParallelForIO(10)
.ExecuteAsync(); // Optimized for I/O with high concurrency

Console.WriteLine("\nAll examples completed!");
}

private static async IAsyncEnumerable<int> GenerateAsyncNumbers(int count)
{
for (int i = 1; i <= count; i++)
{
await Task.Yield(); // Simulate async generation
yield return i;
}
}

private static async Task SimulateApiCall(int id)
{
await Task.Delay(Random.Shared.Next(10, 50));
}

private static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
{
var list = new List<T>();
await foreach (var item in source)
{
list.Add(item);
}
return list;
}
}
4 changes: 4 additions & 0 deletions EnumerableAsyncProcessor.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ Task<HttpResponseMessage> PingAsync()
#if NET6_0_OR_GREATER
// Run channel processing examples
await ChannelProcessingExamples.RunAllExamples();

// Run IAsyncEnumerable examples
Console.WriteLine("\n\n=== Running IAsyncEnumerable Examples ===\n");
await AsyncEnumerableExample.RunExamples();
#endif
Loading