Article ID: 926499 - Last Review: November 7, 2006 - Revision: 1.2

How to implement an InvokeWorkflow completed event in Windows Workflow Foundation

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

On This Page

Expand all | Collapse all

INTRODUCTION

This article describes how to implement an InvokeWorkflow completed event. This article includes the following sections:
  • Add code for the InvokeWorkflow completed event.
  • Check the completion.

MORE INFORMATION

When you use an activity that has an InvokeWorkflow control in a workflow, the workflow completed event happens after the invoked workflow is completed or after the invoker workflow is completed.

Add code for the InvokeWorkflow completed event

When you use the following code for the completed event, the program stops after one workflow is completed.
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{
    waitHandle.Set();
};
If you want to see the execution and the result of all the workflows that are running, you have to set the WaitHandle event for each workflow. This includes the invoked workflows and the invoker workflow. You then have to set the WaitOne event for each workflow.

Note You can create the WaitHandle event together with the AutoResetEvents event by using the following code.
AutoResetEvent waitHandle = new AutoResetEvent(false);
For a workflow that has one invoke workflow, you can use the following code to implement the workflow.
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{
    waitHandle.Set();
};
// In the hosting code, wait two times.
waitHandle.WaitOne();   // This waits for the invoked workflow to be completed.
waitHandle.WaitOne();   // This waits for the host workflow to be completed.
Note Two workflows are completed: the hosting workflow and the invoked workflow.

Check the completion

To check a completion for a particular instance, use the following code.
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{

   if (e.WorkflowInstance.InstanceId == DESIRED_INSTANCE_ID)
       waitHandle.Set();
};
For the main workflow, you can use the WorkflowInstance.InstanceId property to check the completion of the invoker workflow.

APPLIES TO
  • Windows Workflow Foundation
Keywords: 
kbhowto KB926499