mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-10 08:53:06 +00:00
Rework architecture
This commit is contained in:
32
DiscordChatExporter.Domain/Utilities/AsyncExtensions.cs
Normal file
32
DiscordChatExporter.Domain/Utilities/AsyncExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiscordChatExporter.Domain.Utilities
|
||||
{
|
||||
public static class AsyncExtensions
|
||||
{
|
||||
public static async Task ParallelForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> handleAsync, int degreeOfParallelism)
|
||||
{
|
||||
using var semaphore = new SemaphoreSlim(degreeOfParallelism);
|
||||
|
||||
await Task.WhenAll(source.Select(async item =>
|
||||
{
|
||||
// ReSharper disable once AccessToDisposedClosure
|
||||
await semaphore.WaitAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await handleAsync(item);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// ReSharper disable once AccessToDisposedClosure
|
||||
semaphore.Release();
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user