I would like to know if there is maybe a finally that i can use in a Parallel.ForEach?
try
{
}
finally
{
}
Is this possible in a Parallel.ForEach?
I need to do a finally when the loop has completed.
Parallel.ForEach(someList, x =>
{
//...
}, // Now i need to do a finally);
My problem is that i have a try finally around my Parallel.ForEach, but i dont want the finally to happen. It must only happen when the parallel task has completed.
So this does not work for me:
try
{
Parallel.ForEach(someList, x =>
{
//...
});
}
finally
{
}
Is there maybe another way of doing this?
Loops and
finallydon’t go together (except maybe in Python). You simply write the code to run after the loop, well, after the loop.If you want to run it regardless of exceptions in the loop, do it like this:
On second thought, maybe the OP ment to use this Parallel.ForEach overload, which allows you to provide a “localFinal” action, that is invoked whenever a thread (used to execute one more iterations of the loop) has completed.