workflow-foundation-4 - 如何在 WF 4 中执行异步事件时控制线程数

标签 workflow-foundation-4

我正在 WF 4 中创建一个工作流程,其中有一个 ParallelForeach 事件,用于迭代项目集合。对于集合中的每个项目,我执行自定义异步事件来并行处理多个项目。

上述解决方案适用于我,但我担心使用的线程数,因为每个异步事件实例都是在其自己的线程上执行的。有没有办法配置/控制在上述机制中执行parallelForeach事件时启动的线程数量?

最佳答案

因为每个异步事件实例都在其自己的线程上执行。谁说? Certainly not the docs.

ParallelForEach enumerates its values and schedules the Body for every value it enumerates on. It only schedules the Body. How the body executes depends on whether the Body goes idle. If the Body does not go idle, it executes in a reverse order because the scheduled activities are handled as a stack, the last scheduled activity executes first.

For example, if you have a collection of {1,2,3,4}in ParallelForEach and use a WriteLine as the body to write the value out. You have 4, 3, 2, 1 printed out in the console. This is because WriteLine does not go idle so after 4 WriteLine activities got scheduled, they executed using a stack behavior (first in last out).

仅当事件创建书签并进入空闲状态时,才会发生并行执行。即使如此,两项事件实际上并没有同时执行——一项或多项事件刚刚停止执行,允许其他事件按顺序运行。考虑到这个名字,可以理解地令人困惑,但仅此而已。

无论如何,当您依赖框架为您进行并行化时,不必担心它们使用了多少线程。或许一切都在他们的掌控之中。直到您知道他们没有。

关于workflow-foundation-4 - 如何在 WF 4 中执行异步事件时控制线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14146348/

相关文章:

workflow-foundation-4 - Windows Workflow 4.0 中特定于区域性的 XAML 工作流

c# - Windows Workflow Foundation - 获取已执行事件的列表?

workflow - WF 4.0 资源合集

wcf - 将 ChannelFactory 与 WorkflowServiceHost 一起使用

c# - 服务器重启时如何从实例存储中加载具有多个工作流定义的可运行实例

workflow-foundation-4 - 列出当前在 .Net 4.0 中运行的工作流

c# - WF4 While 代码中的事件条件

wcf - 如何在发送到通过 MQ 激活的 WF 服务的 MSMQ 消息的客户端设置优先级

workflow-foundation-4 - 在 Windows Workflow 4.5 中写入输出文件

.net - Windows 工作流是否可以封装到 Web 服务中,以便任何类型的客户端都可以触发它?