简体   繁体   English

执行任务列表时出现问题,每个任务中都有多个异步调用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM