For each randomly freeze

Why does my parallel for each randomly freeze even though each branch has no ui interaction??
Thanks

Hi @manish.pal ,

Try this approach

  • Use local variables inside each parallel branch
  • Avoid modifying the same collection inside parallel
  • Use ConcurrentDictionary or ConcurrentBag for thread-safe items
  • Or create a local copy for each iteration.
    cheers

Parallel.ForEach can freeze even without UI interaction because the loop threads may block each other. This usually happens due to hidden async deadlocks, lock contention on shared resources, blocking I/O inside the loop, or exceptions that delay completion. Even if UI is not touched, any blocking call inside the loop can cause the thread pool to stall.

@manish.pal

May I know what you are doing in parallel for each..are there any shared resources that you are using?

cheers

Thanks for the inputs.
After debugging the braches invidually, i identified that the freeze was caused by a silent exception inside one of the parallel braches. since Countinue On Error was enabled, the exception didn’t surface, which caused a thread stall at the parallel schedular level. Disabling COE exposed the exception, and onnce that branch was corrected, the parallel forrach executed without blocking.

Thanks again for the help.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.