Add managed cancellation support

Closes #716
This commit is contained in:
Tyrrrz
2021-10-07 17:06:05 +03:00
parent 2f3e165988
commit 21d89afa70
21 changed files with 274 additions and 147 deletions

View File

@@ -27,14 +27,15 @@ namespace DiscordChatExporter.Core.Utils.Extensions
public static async ValueTask ParallelForEachAsync<T>(
this IEnumerable<T> source,
Func<T, ValueTask> handleAsync,
int degreeOfParallelism)
int degreeOfParallelism,
CancellationToken cancellationToken = default)
{
using var semaphore = new SemaphoreSlim(degreeOfParallelism);
await Task.WhenAll(source.Select(async item =>
{
// ReSharper disable once AccessToDisposedClosure
await semaphore.WaitAsync();
await semaphore.WaitAsync(cancellationToken);
try
{