mirror of
https://github.com/WowUp/WowUp.git
synced 2026-04-23 07:17:00 -04:00
24 lines
693 B
C#
24 lines
693 B
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WowUp.WPF.Extensions
|
|
{
|
|
public static class TaskExtensions
|
|
{
|
|
public static Task ForEachAsync<T>(this IEnumerable<T> source, int dop, Func<T, Task> body)
|
|
{
|
|
return Task.WhenAll(
|
|
from partition in Partitioner.Create(source).GetPartitions(dop)
|
|
select Task.Run(async delegate
|
|
{
|
|
using (partition)
|
|
while (partition.MoveNext())
|
|
await body(partition.Current);
|
|
}));
|
|
}
|
|
}
|
|
}
|