C
C#3y ago
Gladiator

❔ 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);
}
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);
}
30 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
Yes, I said Task.Run is OK but without it, it is not called at all Using await await for Task.WhenAny because it returns Task<Task<>>
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
list.Add(new Task<PathResult>(action)); It is 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
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
I have tested, my above code is not called . I have put break points here var pathResult = _pathFinder.FindPath
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
REPL Result: Success
var roadPoints = new int[] { 1, 2 };
var tasks = roadPoints.Select(roadPoint =>
{
var isThatActuallyNeeded = roadPoint;
return Task.Run(() =>
{
return isThatActuallyNeeded * 100;
});
}).ToArray();

await Task.WhenAll(tasks);
(tasks[0].Result, tasks[1].Result)
var roadPoints = new int[] { 1, 2 };
var tasks = roadPoints.Select(roadPoint =>
{
var isThatActuallyNeeded = roadPoint;
return Task.Run(() =>
{
return isThatActuallyNeeded * 100;
});
}).ToArray();

await Task.WhenAll(tasks);
(tasks[0].Result, tasks[1].Result)
Result: ValueTuple<int, int>
{
"item1": 100,
"item2": 200
}
{
"item1": 100,
"item2": 200
}
Compile: 639.115ms | Execution: 99.819ms | React with ❌ to remove this embed.
Gladiator
GladiatorOP3y ago
😐
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
REPL Result: Failure
var roadPoints = new int[] { 1, 2 };
var tasks = roadPoints.Select(roadPoint =>
{
var isThatActuallyNeeded = roadPoint;
Task.Run(() =>
{
return isThatActuallyNeeded * 100;
});
}).ToArray();

await Task.WhenAll(tasks);
(tasks[0].Result, tasks[1].Result)
var roadPoints = new int[] { 1, 2 };
var tasks = roadPoints.Select(roadPoint =>
{
var isThatActuallyNeeded = roadPoint;
Task.Run(() =>
{
return isThatActuallyNeeded * 100;
});
}).ToArray();

await Task.WhenAll(tasks);
(tasks[0].Result, tasks[1].Result)
Exception: CompilationErrorException
- The type arguments for method 'Enumerable.Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
- The type arguments for method 'Enumerable.Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Compile: 638.056ms | Execution: 0.000ms | React with ❌ to remove this embed.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
What a terrible design It gets Task but it does not work, really shit design OK, thanks dudes
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
If I call Start, it works. Yes, those tasks are required to be started Appreciated
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
Now, it is like Task.Run in terms of multi threading? it can be executed in different threads or awesome
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
OK, I test it By the way, in my testing Task.Run is handling it very well 10 tasks around 100 sec but now it is 11 sec
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
Yes, faster is better 🙂
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
Because, sometimes a path can take huge time
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Gladiator
GladiatorOP3y ago
Maybe, but I am tireddd It is A* algorithm path finding with large path length
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
I'm not aware of such a thing.
Gladiator
GladiatorOP3y ago
At least, I can run the algorithm with each target point parallelly. It is my first simplest option
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server