C#
C#

help

Root Question Message

Gladiator
Gladiator11/18/2022
❔ Task.WhenAll does not work without Task.Run inside

Inside a task, _pathFinder.FindPath is not called.
returnFirstPath is false (Task.WhenAll)
but if I change list.Add(new Task<PathResult>(action)); to 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);
                }
tebeco
tebeco11/18/2022
your code have several issue here
tebeco
tebeco11/18/2022
* avoid new Task => Task.Run
* you have await await
* I don't see any run of the Task because you should not use new Task
* the if/else is redundant and pathResult probably too
Message Not Public

Sign In and Join Server To See

11/18/2022
tebeco
tebeco11/18/2022
var tasks = roadPoints.Select(roadPoint => 
{
  var isThatActuallyNeeded = roadPoint;
  Task.Run(() =>
  {
    return _pathFinder.FindPath(
        sourcePoint, setting.SearchRange,
        isThatActuallyNeeded , CheckTarget, CheckObstacle, ComputeWeight
    );
  }
});

var pathResult = returnFirstPath
  ? await Task.WhenAny(tasks)
  : (await Task.WhenAll(list)).MinBy(p => p.Path.Length);
tebeco
tebeco11/18/2022
something like that
tebeco
tebeco11/18/2022
that's from discord text field, you should expect some typo / error
Message Not Public

Sign In and Join Server To See

11/18/2022
Gladiator
Gladiator11/18/2022
Yes, I said Task.Run is OK but without it, it is not called at all
Gladiator
Gladiator11/18/2022
Using await await for Task.WhenAny because it returns Task<Task#>
tebeco
tebeco11/18/2022
because yhere's no element in the array ?
Message Not Public

Sign In and Join Server To See

11/18/2022
tebeco
tebeco11/18/2022
you should not
tebeco
tebeco11/18/2022
and it should not
Gladiator
Gladiator11/18/2022
list.Add(new Task<PathResult>(action));
Gladiator
Gladiator11/18/2022
It is
Gladiator
Gladiator11/18/2022
I want to know if I can use Task.WhenAll without Task.Run or not, surely I should be able to use it with simple tasks
Message Not Public

Sign In and Join Server To See

11/18/2022
tebeco
tebeco11/18/2022
my code forgot to do a return
Message Not Public

Sign In and Join Server To See

11/18/2022
Gladiator
Gladiator11/18/2022
I have tested, my above code is not called
Gladiator
Gladiator11/18/2022
.
Message Not Public

Sign In and Join Server To See

11/18/2022
Gladiator
Gladiator11/18/2022
I have put break points here var pathResult = _pathFinder.FindPath
tebeco
tebeco11/18/2022
code works
tebeco
tebeco11/18/2022
have a good night
tebeco
tebeco11/18/2022
proof bellow
tebeco
tebeco11/18/2022
the task runs
Gladiator
Gladiator11/18/2022
😐
tebeco
tebeco11/18/2022
as pointed
return Task.Run <== the return was missing
tebeco
tebeco11/18/2022
but it should not have built I guess you would have had a compiler error
tebeco
tebeco11/18/2022
sooooo
tebeco
tebeco11/18/2022
did you tried the code 😄 ?
tebeco
tebeco11/18/2022
this is what it should have said when you tried my initial code
tebeco
tebeco11/18/2022
if you did not see that error message you tried a different code
tebeco
tebeco11/18/2022
Task.Run ... literally ALWAYS run
tebeco
tebeco11/18/2022
it's done on purpose
tebeco
tebeco11/18/2022
because you're not supposed to manipulate a Task that's in such a weird state that new Task does
Message Not Public

Sign In and Join Server To See

11/18/2022
tebeco
tebeco11/18/2022
because new Task has NEVER been designed for Async
tebeco
tebeco11/18/2022
there's a shit load tons of difference
tebeco
tebeco11/18/2022
like the continuation
wrap/unwrap
error handling
scheduler
....
Message Not Public

Sign In and Join Server To See

11/18/2022
Gladiator
Gladiator11/18/2022
What a terrible design
Gladiator
Gladiator11/18/2022
It gets Task but it does not work, really shit design
Gladiator
Gladiator11/18/2022
OK, thanks dudes
Message Not Public

Sign In and Join Server To See

11/18/2022
tebeco
tebeco11/18/2022
yes that's why BCL team said they regret this new Task ctor thing ... exists
tebeco
tebeco11/18/2022
it's an abomination
Message Not Public

Sign In and Join Server To See

11/18/2022
tebeco
tebeco11/18/2022
and you have to check IsCancelled
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy