user4951
user4951

Reputation: 33060

How to do a task after another task in vb.net?

    For Each account In _accounts11
        Dim newtask = account.readbalancesAsync()
        newtask = newtask.ContinueWith(Sub() account.LogFinishTask("Getting Balances", starttime))
        newtask = newtask.ContinueWith(Async Function() account.getOrdersAsync())
        newtask = newtask.ContinueWith(Sub() account.LogFinishTask("Getting Orders", starttime))

        tasklist.Add(newtask)
    Next


    Await Task.WhenAll(tasklist.ToArray)
    Dim b = 1

Basically, for each account, I want to do account.readbalancesAsync and after that, I want to do account.getOrdersAsync()

I left code newtask.ContinueWith(Sub() account.LogFinishTask("Getting Balances", starttime)) to show I know how ContinueWith works. However, after that, I need to continue with another task.

How do I do so?

What I am trying to do is something like this

    For Each account In _accounts11
        await account.readbalancesAsync()
        account.LogFinishTask("Getting Balances", starttime)
        await account.getOrdersAsync())
        account.LogFinishTask("Getting Orders", starttime)

        tasklist.Add(newtask)
    Next

Obviously, if I do it like this, then one account have to wait for another account to finish. I want all accounts to run parallelly.

Or let's take a look at this code

dim response1 = await client.GetAsync("http://example.com/");
dim response2 = await client.GetAsync("http://stackoverflow.com/");

Say I do it like this

dim newtask = client.GetAsync("http://example.com/").continueWith(....)
await newtask

What should I put in ....

Upvotes: 0

Views: 941

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18310

I think you've mistakenly taken a wrong turn somewhere. If you need to run those four statements after each other, but without interfering with the loop, all you need to do is to create one task executing a multiline/block lambda expression.

For instance:

For Each account In _accounts11
    Dim newtask = Task.Run( 'Start a new task.
        Async Function() 'Multiline lambda expression.
            Await account.readbalancesAsync()
            account.LogFinishTask("Getting Balances", starttime)
            Await account.getOrdersAsync()
            account.LogFinishTask("Getting Orders", starttime)
        End Function
    ) 'End of Task.Run()

    tasklist.Add(newtask)
Next

Upvotes: 3

user4951
user4951

Reputation: 33060

I just want to add something to VisualVincent's answer. I still prefer to do this with continueWith

Private Async Function readBalancesAndOrderForEachAccount(starttime As Long) As Task
    Await readbalancesAsync()
    LogFinishTask("Getting Balances", starttime)
    Await getOrdersAsync()
    LogFinishTask("Getting Orders", starttime)
End Function

Public Shared Async Function getMarketDetailFromAllExchangesAsync2() As Task
    Dim CurrentMethod = MethodBase.GetCurrentMethod().Name
    Dim tasklist = New List(Of Task)
    Dim starttime = jsonHelper.currentTimeStamp

...

        For Each account In _accounts11
            Dim newtask = account.readBalancesAndOrderForEachAccount(starttime)
            tasklist.Add(newtask)
        Next
        Await Task.WhenAll(tasklist.ToArray)
        Dim b = 1
   ...
    End Function

That seems work. However, I want to understand how to do this using continueWith because I am very curious.

Upvotes: 1

Related Questions