【发布时间】:2019-04-29 19:23:55
【问题描述】:
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
基本上,对于每个帐户,我都想做 account.readbalancesAsync,然后再做 account.getOrdersAsync()
我留下了代码 newtask.ContinueWith(Sub() account.LogFinishTask("Getting Balances", starttime)) 以表明我知道 ContinueWith 的工作原理。但是,在那之后,我需要继续执行另一项任务。
我该怎么做?
我想做的是这样的
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
显然,如果我这样做,那么一个帐户必须等待另一个帐户完成。我希望所有帐户并行运行。
或者我们看看这段代码
dim response1 = await client.GetAsync("http://example.com/");
dim response2 = await client.GetAsync("http://stackoverflow.com/");
说我是这样做的
dim newtask = client.GetAsync("http://example.com/").continueWith(....)
await newtask
我应该放什么......
【问题讨论】:
-
您已经开始多次执行后续任务。是什么阻止您继续拨打
ContinueWith?虽然我真的很想知道你为什么首先使用所有这些延续任务,而不是仅仅将所有内容包装在Sub()/End Sublambda 块中?由于每个任务都已经在后台运行,因此创建更多的任务是没有意义的。 -
基本上,对于每个帐户,我想做await account.readbalancesasync,然后是await account.getordersasync。但是,我希望它们并行运行而不是串行运行。
-
我知道如何将 continueWith 与非阻塞正常功能一起使用。现在我想将 continueWith 与异步函数一起使用。
标签: vb.net task continuewith