Learn Windows Workflow Foundation with me (2) -- let WF receive data through Parameters

Source: Internet
Author: User

In this section, we will learn how to call a workflow through other programs, and transfer some data to the workflow at workflow startup so that it can be used in the workflow.

When workflow starts execution, there are two ways to pass data to the workflow-parameters and events. Today, let's talk about how to pass data to a workflow through parameters.

Parameters to be passed to workflow. We need to set parameters in the workflow first, and then use Dictionary <string, the object> key-value set is passed as follows:

Continue to use the code we used in the previous section and add two attributes FirstName and LastName to the Workflow1 class.

Private string firstName;

Public string FirstName
{
Get
{
Return firstName;
}
Set
{
FirstName = value;
}
}

Private string lastName;

Public string LastName
{
Get
{
Return lastName;
}
Set
{
LastName = value;
}
}

 

Here, we will pass the fristName and lastName through other applications and display these two values in a dialog box during workflow execution.

Because we created a console application in the previous section, the System. Windows. Forms assembly must be applied to the pop-up dialog box.

Then modify codeactivity=codehandler in our Workflow1 class.

 

Private void codeactivityinclucodehandler (object sender, EventArgs e)
{
System. Windows. Forms. MessageBox. Show (
"Hello world:" + firstName + "" + lastName );
}

Now we have modified the workflow code.

 

Next we will create a winForm application.

Add a new projectWinFormTestHost.

Next, confirm that the newly created Project WinFormTestHost can call workflow Workflow1.

Add a reference to WinFormTestHost to reference the HelloWorldWorkflow project.

At the same time, we also want our winfrom application to reference the wwf assembly. Continue adding references, select the. net tab, and add the following three items:

System. Workflow. Activities

System. Workflow. ComponentModel

System. Workflow. Runtime

 

Modify the Form1 form.

Add two labels: Label1 and Label2, and the Text attributes are First name and LastName.

Add two TextBox names: txtFirstName and txtLastName.

Add a button btnStartWorkflow to Start our Workflow. The Name attribute is btnStartWorkflow, and the Text attribute is Start Workflow.

This simple form is complete. Let's add the code.

First, add the workflow runtime declaration in our Form1.cs class:

Private WorkflowRuntime wr;

 

Double-click btnStartWorkflow to add an event handler.

 

Private void btnStartWorkflow_Click (object sender, EventArgs e)
{
If (wr = null)
{
Wr = new WorkflowRuntime ();
Wr. StartRuntime ();
}

// Define a set of keys and values (Dictionary) to pass Parameters
Dictionary <string, object> parameters = new Dictionary <string, object> ();
Parameters. Add ("FirstName", txtFirstName. Text );
Parameters. Add ("LastName", txtLastName. Text );

// Create a workflow instance
WorkflowInstance instance = wr. CreateWorkflow (typeof (HelloWorldWorkflow. Workflow1), parameters );
// Start the Workflow
Instance. Start ();

}

Finally, add code to the FormClosed event to close the workflow when closing the form.

 

Private void form=formclosed (object sender, FormClosedEventArgs e)
{
If (wr! = Null)
{
If (wr. IsStarted)
{
Wr. StopRuntime ();
}
}
}

 

Now our program is developed. Click F5 to open the form:

After entering the name, click Start Workflow to bring up the form:

This form is executed in Workflow1. At the same time, we can see that the value entered in the winfrom form is successfully transferred to the workflow.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.