问题
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