© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
94 replies
Gladiator

❔ Task.WhenAll does not work without Task.Run inside

Inside a task,
_pathFinder.FindPath
_pathFinder.FindPath
is not called.
returnFirstPath
returnFirstPath
is false (Task.WhenAll)
but if I change
list.Add(new Task<PathResult>(action));
list.Add(new Task<PathResult>(action));
to
list.Add(Task.Run(action));
list.Add(Task.Run(action));
, it works.
Another question, I should use Task.Run to be sure it runs in different threads and get results faster?
    for (var j = 0; j < roadPoints.Length; j++)
                {
                    var j1 = j;
                    Func<PathResult> action = () =>
                    {
                        var pathResult = _pathFinder.FindPath
                        (
                            sourcePoint,
                            setting.SearchRange,
                            roadPoints[j1],
                            CheckTarget,
                            CheckObstacle,
                            ComputeWeight
                        );
                        return pathResult;
                    };
                    list.Add(new Task<PathResult>(action));
                }

                PathResult pathResult;
                if (returnFirstPath)
                {
                    pathResult = await await Task.WhenAny(list);
                }
                else
                {
                    var pathResults = await Task.WhenAll(list);
                    pathResult = pathResults.MinBy(p => p.Path.Length);
                }
    for (var j = 0; j < roadPoints.Length; j++)
                {
                    var j1 = j;
                    Func<PathResult> action = () =>
                    {
                        var pathResult = _pathFinder.FindPath
                        (
                            sourcePoint,
                            setting.SearchRange,
                            roadPoints[j1],
                            CheckTarget,
                            CheckObstacle,
                            ComputeWeight
                        );
                        return pathResult;
                    };
                    list.Add(new Task<PathResult>(action));
                }

                PathResult pathResult;
                if (returnFirstPath)
                {
                    pathResult = await await Task.WhenAny(list);
                }
                else
                {
                    var pathResults = await Task.WhenAll(list);
                    pathResult = pathResults.MinBy(p => p.Path.Length);
                }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

✅ Using IProgress with await Task.WhenAll(tasks);
C#CC# / help
2y ago
❔ Any way to optimize this? Task.WhenAll
C#CC# / help
3y ago
❔ Task.WhenAll for Tests, and other async questions
C#CC# / help
4y ago