首页 > 解决方案 > Task.WhenAll 多线程按顺序运行而不是并行运行

问题描述

我有一个应用程序,我最初在我所属的所有团队中建立了多少个(微软)团队。
因此,如果我是 37 个团队的成员,我最终需要列出我实际拥有的 13 个团队。

它可以工作 - 查询每个团队的所有者的 MS Graph - 但是一些用户是数百个团队的所有者,很明显,当必须等待顺序加载时加载时间是不可接受的。所以我试图用Task.Selectand来解决这个问题Task.WhenAll。但是,任务按顺序运行,而不是并行运行。
我很有兴趣将总加载时间降低到大约 250 毫秒,而不是 250 乘以 37。我已经读到,task.WhenAll如果我.Result在任务中使用它会很生气,导致它按顺序运行,但我不知道怎么做才能使它在并行线程中运行。

 private static async Task DispatchGetTeamOwnersAsync(JEnumerable<JToken> userTeams, GraphittiBox.Model.TokenObject token)
        {

            var tasks = userTeams.Select(async team =>
            {
                Team t = JsonConvert.DeserializeObject<Team>(team.ToString());
                Stopwatch clock = Stopwatch.StartNew();
                LogService.WriteLog("await: GetOwnersOfTeamAsync");

                
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.Token_type, token.Access_token);
                var url = new Uri("https://graph.microsoft.com/v1.0/groups/{groupId}/owners?$select=mail,id,displayName".Replace("{groupId}", t.Id));
                var response = httpClient.GetAsync(url);

                var content = response.Result.Content.ReadAsStringAsync();

                JObject owners = JsonConvert.DeserializeObject<JObject>(content.Result);

                JsonOwnersCollection.Add(new OwnersAsyncList(t.Id, owners));


                clock.Stop();
                LogService.WriteLog("Done (" + clock.ElapsedMilliseconds.ToString() + " ms)");
            });

            await Task.WhenAll(tasks);

        }

日志文件:

05-03-2021 08:21:04 Info    User initiated 'ProcessForm'
05-03-2021 08:21:05 Info    User has a total of 37 UserJoinedTeams
05-03-2021 08:21:05 Info    Method: GetAsyncOwners (ProcessForm)
05-03-2021 08:21:05 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:06 Info    Done (335 ms)
05-03-2021 08:21:06 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:06 Info    Done (237 ms)
05-03-2021 08:21:06 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:06 Info    Done (231 ms)
05-03-2021 08:21:06 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:07 Info    Done (214 ms)
05-03-2021 08:21:07 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:07 Info    Done (219 ms)
05-03-2021 08:21:07 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:07 Info    Done (229 ms)
05-03-2021 08:21:07 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:07 Info    Done (217 ms)
05-03-2021 08:21:07 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:08 Info    Done (314 ms)
05-03-2021 08:21:08 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:08 Info    Done (225 ms)
05-03-2021 08:21:08 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:08 Info    Done (203 ms)
05-03-2021 08:21:08 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:08 Info    Done (206 ms)
05-03-2021 08:21:08 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:08 Info    Done (251 ms)
05-03-2021 08:21:08 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:09 Info    Done (289 ms)
05-03-2021 08:21:09 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:09 Info    Done (224 ms)
05-03-2021 08:21:09 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:09 Info    Done (258 ms)
05-03-2021 08:21:09 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:10 Info    Done (240 ms)
05-03-2021 08:21:10 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:10 Info    Done (317 ms)
05-03-2021 08:21:10 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:10 Info    Done (297 ms)
05-03-2021 08:21:10 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:10 Info    Done (255 ms)
05-03-2021 08:21:10 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:11 Info    Done (208 ms)
05-03-2021 08:21:11 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:11 Info    Done (243 ms)
05-03-2021 08:21:11 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:11 Info    Done (260 ms)
05-03-2021 08:21:11 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:12 Info    Done (369 ms)
05-03-2021 08:21:12 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:12 Info    Done (248 ms)
05-03-2021 08:21:12 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:12 Info    Done (232 ms)
05-03-2021 08:21:12 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:12 Info    Done (300 ms)
05-03-2021 08:21:12 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:13 Info    Done (233 ms)
05-03-2021 08:21:13 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:13 Info    Done (249 ms)
05-03-2021 08:21:13 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:13 Info    Done (253 ms)
05-03-2021 08:21:13 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:13 Info    Done (262 ms)
05-03-2021 08:21:13 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:14 Info    Done (243 ms)
05-03-2021 08:21:14 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:14 Info    Done (227 ms)
05-03-2021 08:21:14 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:14 Info    Done (235 ms)
05-03-2021 08:21:14 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:14 Info    Done (257 ms)
05-03-2021 08:21:14 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:15 Info    Done (225 ms)
05-03-2021 08:21:15 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:15 Info    Done (231 ms)
05-03-2021 08:21:15 Info    await: GetOwnersOfTeamAsync
05-03-2021 08:21:15 Info    Done (227 ms)
05-03-2021 08:21:15 Info    User is owner of 13 UserJoinedTeams

标签: c#asp.netasynchronousmultitasking

解决方案


您正在使用

var content = response.Result.Content.ReadAsStringAsync();

.Result部分将导致阻塞,直到任务完成。您应该改为等待任务。


推荐阅读