Restrict number of threads in parallel foreach in Windows workflow foundation 4.0

余生颓废 提交于 2019-12-24 13:42:50

问题


I have WF in which I am using parallel foreach to enumerate through a list of items and do some Processing on each item. The requirement is to run the parallel foreach infinitely until the user intervenes and stops the process. However when there are more than 20 items, I see multiple threads are spun off and the CPU usage will spike to 99 percentages and eventually the entire system slows down to respond. How can I limit the number of threads that can be created in parallel foreach of WWF. Is there any property that I can set? Any help is highly appreciated.


回答1:


Create a LimitedConcurrencyLevelTaskScheduler

http://msdn.microsoft.com/en-us/library/ee789351.aspx




回答2:


WF4 doesn't create new threads for the items. Only when async activities are used will you get any overlapping execution of activities. Normally this works best with asynchronous IO but you can do background compilation if you do so in your activity by starting other threads. The WF runtime will only use a single thread per workflow instance though.



来源:https://stackoverflow.com/questions/16365049/restrict-number-of-threads-in-parallel-foreach-in-windows-workflow-foundation-4

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