Parallel.Foreach loop, inconsistent behavior with explicit throw statement

為{幸葍}努か 提交于 2019-12-02 01:14:55

This is entirely expected behaviour.

See the docs,

an unhandled exception causes the loop to terminate immediately

When you throw an exception, no new Tasks will be scheduled.

So the behaviour will appear unpredictable. You have no right to expect that all subtasks will execute. That is not the contract of a Parallel.For loop.

The difference will be much clearer when you add more items to the source list. The output will always show a number of exceptions in the neighbourhood of ThreadPool.MinThreads.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!