Home > OS >  How do I use Parallel.ForEachAsync in VB.NET using .NET 6?
How do I use Parallel.ForEachAsync in VB.NET using .NET 6?

Time:05-28

.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!

CodePudding user response:

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 Async in front of it. Try this

Await Parallel.ForEachAsync(
    userHandlers, 
    parallelOptions, 
    Async Sub(uri, token)
        //stuff
    End Sub))

For more, see async modifier

CodePudding user response:

Alright, so it was a Visual Studio glitch that persisted between restarts...

I did the following:

Await Parallel.ForEachAsync(Of String)(userHandlers, parallelOptions, New Func(Of String, CancellationToken, ValueTask) _
            (Function(uri, token)

Which worked, and then I removed the explicit stuff to get back to this:

 Await Parallel.ForEachAsync(userHandlers, parallelOptions, Function(uri, token)

Which is the exact function that didn't want to work before. Trying to make that nested function Async breaks it again with the same BC36532 error, but not as "permanently" as before. Very weird.

It would seem that maybe VB.NET just doesn't have support for nested Async?

  • Related