A running WF program instance can be suspended by calling the Suspend method of the WorkflowInstance class. A running WF program instance will also be suspended if a SuspendActivity within the WF program is executed. Suspend-Activity is defined in the System.Workflow.ComponentModel namespace and is shown in Listing 5.15. Listing 5.15. SuspendActivity
Use of SuspendActivity in XAML looks like this: <wf:SuspendActivity x:Name="s1" Error="Please fix xyz" /> A suspended WF program instance is not allowed to proceed further in its execution, even if there is work enqueued in its scheduler work queue. The instance remains in a suspended state until the Resume method of its WorkflowInstance is called. The Error property of SuspendActivity (and the equivalent string parameter of the Suspend method of WorkflowInstance) can be used to convey information about why the instance has been suspended. The WorkflowRuntime has a WorkflowSuspended event, which is raised whenever a WF program instance is suspended. Listing 5.16 illustrates this process. Listing 5.16. WF Program Instance Suspension
WorkflowInstance.Suspend stops the execution of the WF program instance, and also causes the WorkflowRuntime to raise the WorkflowSuspended event. The suspended WF program instance remains in memory; if a host application wishes to unload the instance, the Unload method can be called as well (for example, from within the handler of the WorkflowSuspended event). A WF program instance that has been suspended must always be explicitly resumed in order for its execution to continue; it is not enough to simply load a suspended instance that was previously unloaded. Calling Resume on WorkflowInstance will resume the program's execution from wherever it had left off. Calling Resume has no effect in causing the program execution to move forward if there are no items in the WF scheduler's work queue; it only removes a barrier to execution by moving the instance out of the suspended state. |