如何使用.net 6在vb.net中使用Parallel.ForeachAsync?

发布于 2025-02-02 11:51:31 字数 1434 浏览 5 评论 0原文

.NET 6引入 Parallel.ForeachAsync 在C#中运行良好的方法,但是我在vb.net中使用它遇到了问题。

即,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");
});

我无法确定如何将此部分转换为vb.net:

await Parallel.ForEachAsync(userHandlers, parallelOptions, async (uri, token) =>
    {
});

我能想到的最合乎逻辑的转换是这样的:

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

但是,这是不起作用的,丢弃了错误bc36532:nested函数确实没有与代表'func(字符串,concellationToken,valuetask)兼容的签名'

我可以欣赏该方法期望该方法valueTask这样做。使用sub而不是函数也不起作用,也不会将其全部包裹在task中。我一定有一些我想念的东西。

任何帮助将不胜感激!

.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(
quot;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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2025-02-09 11:51:31

好吧,所以这是一个视觉工作室的故障,在重新启动之间持续存在...

我做了以下操作:

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

奏效了,然后我删除了明确的内容以回到这一点:

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

这是确切的功能,以前不想工作。试图使该嵌套函数async使用相同的BC36532错误再次将其打破,但不像以前那样“永久”。很奇怪。

似乎VB.NET可能没有嵌套异步的支持?

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?

烟沫凡尘 2025-02-09 11:51:31

您没有返回任何东西,因此您需要使用sub而不是功能。 VB.NET还具有类似于C#的异步匿名方法,但是您必须在其前面使用键盘async。请尝试

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

更多,请参阅

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文