diff --git a/Shared/Utils/TaskUtils.cs b/Shared/Utils/TaskUtils.cs index dbb2141..8313c0f 100644 --- a/Shared/Utils/TaskUtils.cs +++ b/Shared/Utils/TaskUtils.cs @@ -51,6 +51,33 @@ public static class TaskUtils { public static void ForEach(IEnumerable source, Action body) => Parallel.ForEach(source, body); + public static async Task ForEachAsync(IEnumerable source, int maxDegreeOfParallelism, Func body, + CancellationToken cancellationToken = default, ProgressProvider? progress = null) { + using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + var token = linkedCancellationTokenSource.Token; + var enumeratorLock = new object(); + using var enumerator = source.GetEnumerator(); + async Task WorkerAsync(ProgressProvider? cp) { + try { + while (true) { + T item; + lock (enumeratorLock) { + token.ThrowIfCancellationRequested(); + if (!enumerator.MoveNext()) return; + item = enumerator.Current; + } + await body(item, cp).NoCapture(); + } + } catch { + try { + linkedCancellationTokenSource.Cancel(); // 让尚未开始的 worker 尽快停止 + } catch { + } + throw; + } + } + await TaskUtils.WhenAll(Enumerable.Repeat(WorkerAsync, maxDegreeOfParallelism), progress).NoCapture(); + } /// /// 静默运行程序并等待其结束,返回其输出和退出码。