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

Is there a way to do .ContinueWith(Sub() account.LogFinishTask("GetMarketDetail", starttime)) with addressOf instead?

How?

user4951
  • 32,206
  • 53
  • 172
  • 282

1 Answers1

1

You need to create a method which satisfies any of existed ContinueWith overloads.
In your particular case it should satisfy a signature of Action(Of Task).

But because in ConitnuesWith you are using account instance, you will not be able to use AddressOf with method of the class where loop is executed.

As workaround you can create required method in class of account

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

Usage

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

Suggestion - set Option Strict to On - will saves developer time by displaying possibly errors during compile time, instead of run-time.

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • I want to pass on more parameters than just the task. In fact, the task is not needed at all. – user4951 Apr 29 '19 at 10:33
  • @user4951, that was my point, if you want to use a method which has different signature then lambdas accepted by `ContinueWith`, you should wrap your method with another which will have accepted signature. – Fabio Apr 29 '19 at 19:09
  • `Option Strict On` will make your life much easier, because it will point you into possible type/signature problems much early and saves your time. For example with `Option String On` your code will not compile, because there are no `ContinueWith` overload which accepts `Action` as an argument – Fabio Apr 29 '19 at 19:11