microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.38k stars 2.22k forks source link

Add Task.WaitAll(IList<Task>) #1262

Closed briangru closed 3 years ago

briangru commented 4 years ago

Help me avoid an extra allocation. Why do I need to call .ToArray on a list of tasks when calling WaitAll? Here's a common pattern:

        List<Task> insertItems = new List<Task>();
        try
        {
            foreach(var carState in carStates)
            {
              var insert = ...
              Task task = _azureTable.ExecuteAsync(insert);
              insertItems.Add(task);
            }

            Task.WaitAll(insertItems.ToArray());

Sometimes I need to do this operation under a lock, making this pattern more of a perf problem. Hopefully Stephen and company can come through with a minor API change quickly. Other devs came up with this worse approach.

            Task.WhenAll(insertItems).ConfigureAwait(false).GetAwaiter().GetResult();

or a more elegant reading but more complicated approach:

            await Task.WhenAll(allTasks);

I can't imagine the latter two being as cheap as a properly implemented WaitAll.

kalaskarsanket commented 3 years ago

@briangru is the issue still reproducible ? If so, is this .NET Framework or .NET Core ?

leecow commented 3 years ago

This repo is no longer actively monitored. Closing up old issues that have not had activity in while. If this is still an issue, please open a new issue in an appropriate repo listed in microsoft/dotnet#1275