.NET 6 introduced the Parallel.ForEachAsync method which works pretty well in C#, but I'm running into issues using it in VB.NET.
Namely, the following example in C#:
using HttpClient client = new()
{
BaseAddress = new Uri("https://api.github.com"),
};
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("DotNet", "6"));
ParallelOptions parallelOptions = new()
{
MaxDegreeOfParallelism = 3
};
await Parallel.ForEachAsync(userHandlers, parallelOptions, async (uri, token) =>
{
var user = await client.GetFromJsonAsync<GitHubUser>(uri, token);
Console.WriteLine($"Name: {user.Name}\nBio: {user.Bio}\n");
});
I cannot work out how to convert this section into VB.NET:
await Parallel.ForEachAsync(userHandlers, parallelOptions, async (uri, token) =>
{
});
The most logical conversion I can think of is this:
Await Parallel.ForEachAsync(userHandlers, parallelOptions, Function(uri, token)
//stuff
End Function))
But that does not work, throwing an error BC36532: Nested function does not have a signature that is compatible with delegate 'Func(Of String, cancellationToken, ValueTask)'
I can appreciate that the method expects a ValueTask but I can't work out how to properly do that. Using a Sub instead of Function doesn't work either, neither does wrapping it all in a Task. There has to be something really dumb that I'm missing.
Any help would be greatly appreciated!
Parallel.ForEachAsync()necessary? Can't you awaitTask.WhenAll()instead? -- I don't think theSub()/Function()Lambda has been updated to support an awaitableValueTask. You could return aValueTask, removingasyncfrom the Lambda and unwrapping the Task manually.