[英]Issue when executing a list of tasks, that have multiple async calls inside each one
I got this tentative piece of code below, that should generate a list of tasks to execute concurrently.我在下面得到了这段暂定的代码,它应该生成一个要同时执行的任务列表。 The code inside the try...catch... is mean to represent each of those tasks. try...catch... 中的代码用于表示这些任务中的每一个。 But the code block itself, contains 3 async calls that should execute sequentially, basically the 2nd call depends on the first and the third on the second.但是代码块本身包含 3 个应该按顺序执行的异步调用,基本上第二个调用依赖于第一个,第三个调用依赖于第二个。
var syncActions = new List<SyncAction<ContactDto>>();
ContactDto contactDto = null;
var syncActionTasks = updatedNodes.Select(async node =>
{
SyncAction<IUmbracoActiveCampaignEntity> syncActionResult;
contactDto = Mapper.Map<ContactDto>(node);
try
{
var firstAsyncCallResult = await this.Contacts
.GetByEmailAsync<ContactsRoot>(contactDto.Email);
//...(code omittted for brevity)...
var secondAsyncCallResult = await this.Contacts
.AddOrUpdateAsync(firstAsyncCallResult);
//...(code omittted for brevity)...
var thirdAsyncCallResult = await this.Contacts
.GetAccountContactAssociation(secondAsyncCallResult);
}
catch (ActiveCampaignException ex)
{
// handle error
}
return Task.FromResult(contactDto);
})
.ToList();
var result = await Task.WhenAll(syncActionTasks);
//...(code omittted for brevity)...
What I experience, is that the when the code executes the firstAsyncCallResult
instead of continuing down to the other 2 async calls, it jumps back to the beginning of the code SyncAction<IUmbracoActiveCampaignEntity> syncActionResult;
我的经验是,当代码执行firstAsyncCallResult
而不是继续执行其他 2 个异步调用时,它会跳回到代码的开头SyncAction<IUmbracoActiveCampaignEntity> syncActionResult;
and although all the code runs "fine" the line var result = await Task.WhenAll(syncActionTasks);
尽管所有代码运行“正常”,但行var result = await Task.WhenAll(syncActionTasks);
returns the right number of contactDto
objects, but these objects themselves show up repeated, and not unique as expected.返回正确数量的contactDto
对象,但这些对象本身重复出现,而不是预期的唯一。
Any clue, why and what could be adjusted to get the desired outcome?任何线索,为什么以及可以调整什么以获得预期的结果? Any directions and suggestions will be greatly appreciated.任何指示和建议将不胜感激。
What I experience, is that the when the code executes the firstAsyncCallResult instead of continuing down to the other 2 async calls, it jumps back to the beginning of the code
SyncAction<IUmbracoActiveCampaignEntity> syncActionResult;
我的经验是,当代码执行 firstAsyncCallResult 而不是继续执行其他 2 个异步调用时,它会跳回到代码的开头SyncAction<IUmbracoActiveCampaignEntity> syncActionResult;
Yes, once the first await
is hit, a Task
is returned from the lambda and the next iteration will begin.是的,一旦命中第一个await
,就会从 lambda 返回一个Task
,下一次迭代将开始。 Once the Task
returned from GetByEmailAsync
has completed, the method will resume.从GetByEmailAsync
返回的Task
完成后,该方法将恢复。
Also Task.FromResult
should not be used in an async
method;此外Task.FromResult
不应在async
方法中使用; async
methods implicitly return a Task
, so you do not need to create one yourself. async
方法隐式返回一个Task
,所以你不需要自己创建一个。 Simply return the DTO:只需返回 DTO:
return contactDto;
By using Task.FromResult
the way you have, the lambda's return type is Task<Task<ContactDto>>
.通过使用Task.FromResult
的方式,lambda 的返回类型是Task<Task<ContactDto>>
。
The call to ToList()
is also superfluous, as Task.WhenAll
accepts IEnumerable<Task<TResult>>
which is already returned from your Select
.对ToList()
的调用也是多余的,因为Task.WhenAll
接受IEnumerable<Task<TResult>>
,它已经从您的Select
返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.