.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!
You aren't returning anything, so you need to use Sub instead of Function. VB.Net also has async anonymous methods similar to C#, but you must use the keywoard
Asyncin front of it. Try thisFor more, see async modifier