我正在研究遗留代码库,并且我已将少数数据库调用修改为异步函数。
所以我的DbMethod现在是:
Private Async Function ExecuteDbOperation(ByVal ReturnMessage As String) As Task(Of Integer)
data = Await exec.ExecuteReaderAsync()
End Function
我在点击按钮时调用此方法,所以我有两个解决方案,
Protected Sub btnDecantBarCodeSubmit_Click(sender As Object, e As EventArgs)
var res = ExecuteDbOperation("").Result
End Sub
Protected Async Sub btnDecantBarCodeSubmit_Click(sender As Object, e As EventArgs)
var res = Await ExecuteDbOperation("")
End Sub
如果我将事件处理程序设置为Async,或者我应该选择使用.result,那么现在有交易。
答案 0 :(得分:2)
.Result
。其他所有方法都应标记为SynchronizationContext
或async
。有关详细信息,请参阅Don't Block on Async Code
Async Sub