0
Public Shared Async Function getMarketDetailFromAllExchangesAsync() As Task
    Dim taskList = New List(Of Task)
    Dim starttime = jsonHelper.currentTimeStamp
    LogEvents("Start Getting Market Detail of All")
    For Each account In uniqueAccounts().Values
        Dim newtask = account.getMarketInfoAsync().ContinueWith(Sub() account.LogFinishTask("GetMarketDetail", starttime))
        taskList.Add(newtask)
        'newtask.ContinueWith(Sub() LogEvents(account.ToString))
    Next
    Await Task.WhenAll(taskList.ToArray)
    Dim b = 1
End Function

有没有办法用 addressOf 代替 .ContinueWith(Sub() account.LogFinishTask("GetMarketDetail", starttime)) ?

如何?

4

1 回答 1

1

您需要创建一个满足任何现有ContinueWith重载的方法。
在您的特定情况下,它应该满足Action(Of Task).

但是因为在ConitnuesWith您使用account实例时,您将无法使用AddressOf执行循环的类的方法。

作为解决方法,您可以在类中创建所需的方法account

Public Class Account
    Public Sub LogFinishedMarketDetail(task As Task)
        Dim starttime = jsonHelper.currentTimeStamp
        Me.LogFinishTask("GetMarketDetail, starttime")
    End Sub
End Class

用法

For Each account In uniqueAccounts().Values
    Dim newtask = 
        account.getMarketInfoAsync().ContinueWith(AddressOf account.LogFinishedMarketDetail)
    taskList.Add(newtask)
Next

建议 - 设置Option StrictOn- 将通过在编译时而不是运行时显示可能的错误来节省开发人员的时间。

于 2019-04-28T17:57:28.080 回答