gpt4 book ai didi

vb.net - 在 VB 中正确使用 Async/Await

转载 作者:行者123 更新时间:2023-12-02 00:49:33 24 4
gpt4 key购买 nike

我无法使用 Async/Await 从 WCF 服务获取数据表并将其加载到数据 GridView 中。这是我第一次这样做,我觉得我缺少一些基本的东西。这是我到目前为止的代码:

Private p_oDataService As New SQLService.DataServiceClient

Async Function GetReportDataTable() As Task(Of DataTable)
Try
p_oDataService = New SQLService.DataServiceClient
Dim tDatatable As Task(Of DataTable) = p_oDataService.GetValidationReportsAsync()
Dim dt As DataTable = Await tDatatable
Return dt
Catch ex As Exception
Throw ex
End Try
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim tDT As Task(Of DataTable) = GetReportDataTable()
Dim dt As DataTable = GetReportDataTable.Result
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
Catch ex As Exception
Throw ex
End Try
End Sub

感谢任何帮助!

最佳答案

您正在混合阻塞调用和异步调用。当您调用 GetReportDataTable.Result 时,它会死锁。

事件处理程序允许您等待异步任务以及更新事件处理程序...

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim dt As DataTable = Await GetReportDataTable()
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
Catch ex As Exception
Throw ex
End Try
End Sub

关于vb.net - 在 VB 中正确使用 Async/Await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41064994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com