I'm trying out Parallel.ForEachAsync
and the compiler is kind enough to inform me that the body is a func that returns a ValueTask
, not a Task
.
Stopwatch sw = Stopwatch.StartNew();
var numbers = Enumerable.Range(start: 0, count: 10);
// Error: 'Task WorkAsync(int, CancellationToken)' has the wrong return type
await Parallel.ForEachAsync(
source: numbers,
parallelOptions: new ParallelOptions{ MaxDegreeOfParallelism = 2 },
body: WorkAsync);
async Task WorkAsync(int item, CancellationToken cancellationToken)
{
WriteLine($"Task {item} Start");
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken).ConfigureAwait(false);
WriteLine($"Task {item} End");
}
void WriteLine(string s) => Console.WriteLine($"{sw.ElapsedMilliseconds, 3} Thread{Thread.CurrentThread.ManagedThreadId}: {s}");
A quick search yielded only ForEachAsync examples that use a lambda with multiple statements: async (x, ct) => { ...; await ...; }
.
I feel that
body: async (item, cancellationToken) => await WorkAsync(item, cancellationToken).ConfigureAwait(false));
is significantly uglier than:
body: WorkAsync);
In the trivial example here I can obviously change the return type of the worker method, but it seems unwise to modify existing code to return ValueTask
s.
Is there an obvious solution I have missed for creating beautiful code with Parallel.ForEachAsync
?