200 Views
The sixth part of the Parallel Programming in .NET tutorial is the last instalment that examines the parallel loops. This article discusses how exceptions are thrown by code within a parallel loop and how they can be handled.
Join in on a hand-picked round up of the best .NET articles every day.
Curated by our Moderators and Voted up by our Community. Free.
The sixth part of the Parallel Programming in .NET tutorial is the last instalment that examines the parallel loops. This article discusses how exceptions are thrown by code within a parallel loop and how they can be handled.
How would you go about signaling the other threads that they should stop gracefully if you catch an aggregate exception? Or is there no way to tell scheduled iterations to stop/abort gracefully?
I'm going to write an article outside of the tutorial about this at some point. The best way in a loop is to use parallel loop state (also mentioned in the tutorial - look at the article about terminating loops early). There's a property named IsExceptional that you can look at to see if another parallel iteration has raised an exception. In long-runnning iterations you can check for this and terminate early using Stop.