如何在VB.NET中进行异步并等待工作 [英] How do I make async and await work in VB.NET
本文介绍了如何在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 thetaskList
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屋!
查看全文