任务和WaitAll / WhenAll

时间:2014-08-04 16:34:47

标签: c# asynchronous win-universal-app

你能帮助我加快/优化这段代码:

var keys = xRoot.Elements("key").Select(key => new { ID = int.Parse(key.Attribute("id").Value), VerificationCode = key.Attribute("verificationCode").Value });

var tasks = keys.Select(key => EveOnlineClient.GetAccountStatusAsync(key.ID, key.VerificationCode));

var tasks2 = keys.Select(key => EveOnlineClient.GetAPIKeyInfoAsync(key.ID, key.VerificationCode).ContinueWith(x =>
    {
        var characters = x.Result.Characters;

        var tasks3 = characters.Select(character => EveOnlineClient.GetCharacterSheetAsync(key.ID, key.VerificationCode, character.ID));
        var tasks4 = characters.Select(character => EveOnlineClient.GetSkillInTrainingAsync(key.ID, key.VerificationCode, character.ID));

        Task.WaitAll(tasks3.ToArray());
        Task.WaitAll(tasks4.ToArray());
    }));

await Task.WhenAll(tasks.Concat(tasks2));

1 个答案:

答案 0 :(得分:0)

如果我可以提出一个建议。我发现 WHEN APPLICABLE ,使用列表提供的内容微不足道,但处理速度更快,内存分配更少。 .ToList()虽然它基本上归结为相同的格式,因为它动态地存储它的信息,但从长远来看,往往会提供更少的分配内存。在查看有关Lists vs Arrays的其他问题时,我已经了解到目标和基本上必须保存可能无关紧要的时间和记忆[虽然可能重要时间和记忆,但是要分析你的代码,看看从长远来看哪个更容易证明。