Hope you are all keeping well. I have a question relating to some code I have been reviewing in recent days surrounding Tasks.
var _taskResult = Task.Factory.ContinueWhenAll(arrOfTasks, delCompletedTasks =>
{
try
{
Task.WaitAll(arrOfTasks);
... omitted for brevity
}
catch (AggregateException ex)
{
ex.Handle(x =>
{
... omitted for brevity
}
}
}
What would be the reason for Task.WaitAll(arrOfTasks) if Task.Factory.ContinueWhenAll(...) will wait on all Tasks to complete execution before invoking a delegate?
If I have your question right, you are asking why have two separate commands that both appear to do similar processes?
I believe the purpose of them is similar to how we have While, Loop, and For Each. The differences are subtly different but there are certain situations where one is more suited than the other.
ContinueWhenAll() is used to declare the delegate body that you want executed when all other background threads have completed.
With WaitAll, you are waiting for all threads to complete (or to synchrnonise) at a certain point, sometimes for example, within the delegate method declared by ContinueWhenAll().
Hopefully this following example makes it a little clearer. Good luck.
Thanks for your reply, appreciate it. After some further reading it seems that when 'ContinueWhenAll' completes only then will threads be utilized briefly to process the results, the actual computation work.
The problem I am still having is that even though I receive successful task completion in addition to failed tasks which I handle with the aggregateexception, it stops all the sucessful task results from completing execution. What is the best approach to
continue processing the successful tasks, and handle the failed tasks in a catch block.
Have you considered using the catch block to catch the failed ones, and the finally clause to handle all threads completing successfully? I suggest adding another line below the catch { ... } closing bracket saying finally { ... }. The finally is always
called after the try {...} section has finished execution.
Hope this helps. If not then please clarify what you are trying to achieve. Thank you.
dvLes
Member
83 Points
30 Posts
Parallelism - Task.Factory.ContinueWhenAll and Task.WaitAll
Jun 14, 2012 11:51 AM|LINK
Hi Folks,
Hope you are all keeping well. I have a question relating to some code I have been reviewing in recent days surrounding Tasks.
var _taskResult = Task.Factory.ContinueWhenAll(arrOfTasks, delCompletedTasks => { try { Task.WaitAll(arrOfTasks); ... omitted for brevity } catch (AggregateException ex) { ex.Handle(x => { ... omitted for brevity } } }What would be the reason for Task.WaitAll(arrOfTasks) if Task.Factory.ContinueWhenAll(...) will wait on all Tasks to complete execution before invoking a delegate?
Les
C mvc
Cipher Cogni...
Member
22 Points
7 Posts
Re: Parallelism - Task.Factory.ContinueWhenAll and Task.WaitAll
Jun 14, 2012 03:39 PM|LINK
Hi Les,
If I have your question right, you are asking why have two separate commands that both appear to do similar processes?
I believe the purpose of them is similar to how we have While, Loop, and For Each. The differences are subtly different but there are certain situations where one is more suited than the other.
ContinueWhenAll() is used to declare the delegate body that you want executed when all other background threads have completed.
With WaitAll, you are waiting for all threads to complete (or to synchrnonise) at a certain point, sometimes for example, within the delegate method declared by ContinueWhenAll().
Hopefully this following example makes it a little clearer. Good luck.
http://forums.asp.net/t/1784232.aspx/1
dvLes
Member
83 Points
30 Posts
Re: Parallelism - Task.Factory.ContinueWhenAll and Task.WaitAll
Jun 15, 2012 08:28 AM|LINK
Hi Cipher,
Thanks for your reply, appreciate it. After some further reading it seems that when 'ContinueWhenAll' completes only then will threads be utilized briefly to process the results, the actual computation work.
The problem I am still having is that even though I receive successful task completion in addition to failed tasks which I handle with the aggregateexception, it stops all the sucessful task results from completing execution. What is the best approach to continue processing the successful tasks, and handle the failed tasks in a catch block.
Cipher Cogni...
Member
22 Points
7 Posts
Re: Parallelism - Task.Factory.ContinueWhenAll and Task.WaitAll
Jun 15, 2012 02:22 PM|LINK
Ah I think I understand more now...
Have you considered using the catch block to catch the failed ones, and the finally clause to handle all threads completing successfully? I suggest adding another line below the catch { ... } closing bracket saying finally { ... }. The finally is always called after the try {...} section has finished execution.
Hope this helps. If not then please clarify what you are trying to achieve. Thank you.