如何在VB.NET中进行异步并等待工作 [英] How do I make async and await work in VB.NET

查看:597
本文介绍了如何在VB.NET中进行异步并等待工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Dim  taskList = 列表( of 任务)()

对于 每个 pair As KeyValuePair( Of String 字符串
taskList.Add(Task.Factory.StartNew($ b $) b Sub ()
geXml.Execute()//返回List( of 字符串
结束 Sub
下一步
案例如何实现异步等待调用,因为我 使用此列表( String )因为 从存储过程中获取数据。





我尝试了什么:



每对作为KeyValuePair(Of String,String)成对

taskList.Add (Task.Factory.StartNew(

Sub()

geXml.Execute()//返回List(Of String)

End Sub

下一步

解决方案

假设您要等待 taskList 并获得结果,这样的事情应该有效:

  Dim  taskList 作为 列表(任务( 列表( of   String  )))()

对于 每个 As KeyValuePair( Of String String
taskList.Add(Task.Run( Function ()geXml.Execute()))
下一步

Dim results 作为 IList( IList( Of 字符串))= 等待 Task.WhenAll(taskList)


 Dim taskList = New List(Of Task)()

 For Each pair As KeyValuePair(Of String, String) In pairs
 taskList.Add(Task.Factory.StartNew(
 Sub() 
 geXml.Execute()//which returns List(Of String) 
 End Sub
 Next
In this case how can I implement Async Wait call, because I have to use this List(Of String) becasue it is getting data from storage procedure.



What I have tried:

For Each pair As KeyValuePair(Of String, String) In pairs
taskList.Add(Task.Factory.StartNew(
Sub()
geXml.Execute()//which returns List(Of String)
End Sub
Next

解决方案

Assuming you want to await the taskList and get the results, something like this should work:

Dim taskList As New List(Of Task(Of List(Of String)))()

For Each pair As KeyValuePair(Of String, String) In pairs
    taskList.Add(Task.Run(Function() geXml.Execute()))
Next

Dim results As IList(Of IList(Of String)) = Await Task.WhenAll(taskList)


这篇关于如何在VB.NET中进行异步并等待工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆