The Parallel.ForAsync API was introduced in .NET 8. It has the three following overloads:
public static Task ForAsync<T>(T fromInclusive, T toExclusive,
Func<T, CancellationToken, ValueTask> body)
where T : notnull, IBinaryInteger<T>;
public static Task ForAsync<T>(T fromInclusive, T toExclusive,
CancellationToken cancellationToken, Func<T, CancellationToken, ValueTask> body)
where T : notnull, IBinaryInteger<T>;
public static Task ForAsync<T>(T fromInclusive, T toExclusive,
ParallelOptions parallelOptions, Func<T, CancellationToken, ValueTask> body)
where T : notnull, IBinaryInteger<T>;
It is the first public .NET API to be based on the generic math interfaces introduced in .NET 7.
Usage example:
ParallelOptions options = new() { MaxDegreeOfParallelism = 2 };
await Parallel.ForAsync(0, 100, options, async (i, ct) =>
{
// Process the i element (here the type of i is int)
});
More information and benchmarks about this API can be found in this Microsoft document. Also here is the API proposal.