共用方式為


Understanding the Activity State Model

There are six states an activity can be in during its lifetime. These states are Initialized, Executing, Canceling, Closed, Compensating, and Faulting.

During the Initialized state, an ActivityExecutionContext has been created for the activity and other initiation details specific to that activity have been executed. For example, certain Windows Workflow Foundation activities, such as SuspendActivity, check whether they have a parent composite activity during initialization.

When an activity enters the Executing state, the primary functionality of that activity is performed.

An activity is put into the Canceling state by a parent activity explicitly, or because an exception was thrown during the execution of that activity.

The Closed state is the last and final state of an activity. The one caveat to this is if an activity successfully completes, but then must go through the Compensating state based on business logic. The activity will then transition from Closed to Compensating and then back to Closed once the compensation logic has completed. For more information on compensation, see Using Compensation in Workflows and Using the CompensateActivity Activity.

If an exception is thrown during the Executing state, Canceling state, or Compensating state of an activity, it will transition to the Faulting state.

The following flowchart shows how an activity transitions between the various activity states.

Diagram of the activity state model

The solid red line represents the workflow runtime engine is responsible for transitioning an activity from the Initialized state to the Executing state, or transitioning from the Closed state to the Compensating state.

The solid yellow line represents the parent activity is responsible for transitioning a child activity from the Executing state to the Closed state. If you create a custom composite activity, you must handle this yourself.

The solid blue line represents the workflow runtime engine is responsible for transitioning an activity from the Executing, Canceling, or Compensating state to the Faulting state.

The dashed yellow line represents the workflow runtime engine is responsible for transitioning an activity from either the Canceling state, Compensating state, or Faulting state to the Closed state.

Note

An activity cannot move from the Closed state to the Executing state. Any attempt to do so will cause an exception to be thrown from the call to Execute.

An activity may only close when all children activities are either in their Closed or Initialized states. Because this is a recursive rule, it means that the entire tree below an activity, which is attempting to close, must be Closed or Initialized for the call to succeed.

See Also

Concepts

Understanding the Activity Execution Context

Other Resources

Windows Workflow Foundation Activities