Task 3: Modify the Custom Activity Host Application
In the final step of this tutorial, you modify the host application to start the workflow when the Go button is clicked. The host application passes the URL that is requested by the user, by using the Parameters collection of the WebTearWorkflow sequential workflow. To retrieve the text associated with that URL, the host application creates an event handler for the WorkflowCompleted event. The WorkflowCompletedEventArgs parameter for this event contains the text for the URL. This text is displayed to the user.
Note
Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.
To start the WebTearWorkflow sequential workflow
In the MainForm class, create a private WorkflowRuntime field named workflowRuntime.
private WorkflowRuntime workflowRuntime;
In the MainForm class constructor, create a new instance of the workflowRuntime object.
workflowRuntime = new WorkflowRuntime();
Call the StartRuntime method of the workflowRuntime object to start the Windows Workflow Foundation runtime engine.
workflowRuntime.StartRuntime();
Create an event handler for the WorkflowCompleted event.
The event handler is a generic EventHandler delegate of the WorkflowCompletedEventArgs type. Give this event handler the method name, workflowRuntime_WorkflowCompleted.
workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs> (workflowRuntime_WorkflowCompleted);
In the workflowRuntime_WorkflowCompleted method in the MainForm class, add an If statement to determine whether the InvokeRequired property is true for the data TextBox control:
If the InvokeRequired property of the data TextBox control is true, call the Invoke method of the data TextBox control, passing a new generic EventHandler of the WorkflowCompletedEventArgs type and the workflowRuntime_WorkflowCompleted method name as a parameter to the event handler constructor.
If the InvokeRequired property is false, set the Text property of the data TextBox control equal to the "Data" value of the OutputParameters collection defined in the WorkflowCompletedEventArgs parameter.
private void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { // Retrieve the downloaded page data if (data.InvokeRequired) data.Invoke(new EventHandler<WorkflowCompletedEventArgs> (workflowRuntime_WorkflowCompleted), sender, e); else data.Text = e.OutputParameters["Data"].ToString(); }
In the goButton_Click method, create a new Type object named type, and set it equal to the Type object of the WebTearWorkflow class.
Type type = typeof(WebTearWorkflow);
Create a new generic Dictionary object named properties of objects with String keys.
Dictionary<string, object> properties = new Dictionary<string, object>();
Call the Add method of the properties object, passing the string "Url" and the Text property of the address TextBox control as parameters.
properties.Add("Url", address.Text);
Call the CreateWorkflow method that is defined in the workflowRuntime object, passing the type and properties local variables as parameters.
Call the Start method that is defined in the object that is returned from the CreateWorkflow method.
workflowRuntime.CreateWorkflow(type, properties).Start();
Build and run the application.
Compiling the Code
For information about compiling your code, see Compiling the Code.
For a finished version of the tutorial, see Completed Custom Activity Tutorial.
See Also
Reference
WorkflowRuntime
WorkflowInstance
CreateWorkflow
Start
WorkflowCompleted
WorkflowCompletedEventArgs
Other Resources
Hosting
Tutorial: Host the WF Runtime
Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04