Why does this Parallel.ForEach code freeze the program up?

You must not start the parallel processing in your UI thread. See the example under the “Avoid Executing Parallel Loops on the UI Thread” header in this page.

Update: Or, you can simply create a new thread manuall and start the processing inside that as I see you have done. There’s nothing wrong with that too.

Also, as Jim Mischel points out, you are accessing the lists from multiple threads at the same time, so there are race conditions there. Either substitute ConcurrentBag for List, or wrap the lists inside a lock statement each time you access them.

Leave a Comment