如何使用.net 6在vb.net中使用Parallel.ForeachAsync?
.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,所以这是一个视觉工作室的故障,在重新启动之间持续存在...
我做了以下操作:
奏效了,然后我删除了明确的内容以回到这一点:
这是确切的功能,以前不想工作。试图使该嵌套函数
async
使用相同的BC36532
错误再次将其打破,但不像以前那样“永久”。很奇怪。似乎VB.NET可能没有嵌套异步的支持?
Alright, so it was a Visual Studio glitch that persisted between restarts...
I did the following:
Which worked, and then I removed the explicit stuff to get back to this:
Which is the exact function that didn't want to work before. Trying to make that nested function
Async
breaks it again with the sameBC36532
error, but not as "permanently" as before. Very weird.It would seem that maybe VB.NET just doesn't have support for nested Async?
您没有返回任何东西,因此您需要使用sub而不是功能。 VB.NET还具有类似于C#的异步匿名方法,但是您必须在其前面使用键盘
async
。请尝试更多,请参阅
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 thisFor more, see async modifier