第二次开展儿童活动

时间:2009-11-26 20:55:08

标签: workflow workflow-foundation workflow-activity

我正在尝试使用WF3.5编写自定义CompositeActivity。类似于WhileActivity的东西。

问题是当我想再次执行子活动时(它之前成功调用了ActivityExecutionContext.CloseActivity())我得到一个InvalidOperationException,并显示以下消息:“Activity status必须'Initialized'才能执行”

从调试问题开始,看来子活动的ExecutionStatus已关闭且其ExecutionResult未初始化。

但是ActivityExecutionContext.ExecuteActivity的MSDN文档说“如果状态为Closed,则会初始化并执行活动”。

那么为什么我会得到这个例外,我该如何避免呢?

谢谢, 于连

1 个答案:

答案 0 :(得分:1)

我想我找到了答案。每次想要启动子活动时,复合活动都需要为子活动创建新的ActivityExecutionContext。

在新的ActivityExecutionContext中执行子活动:

ActivityExecutionContext context2 = executionContext.ExecutionContextManager.CreateExecutionContext(a);

// listen for the child's activity completion by implementing IActivityEventListener<ActivityExecutionStatusChangedEventArgs>
context2.Activity.RegisterForStatusChange(Activity.ClosedEvent, this);
context2.ExecuteActivity(context2.Activity);

活动结束后清理:

// get the activity from the ActivityExecutionStatusChangedEventArgs, then...
activity.UnregisterForStatusChange(Activity.ClosedEvent, this);
ActivityExecutionContextManager executionContextManager = executionContext.ExecutionContextManager;

// close the child activity's execution context
executionContextManager.CompleteExecutionContext(executionContextManager.GetExecutionContext(activity));