WF4.0 BASICS (iii) process instance WorkflowApplication and basic principles for designing WF programs

Source: Internet
Author: User

 

Through this section, you can get a preliminary understanding of the features of the WF program and provide a preliminary perceptual knowledge for future WF application development.

 

Directory

1. workflow instance WorkflowApplication and basic principles for designing WF programs 1

1.1 process instance WorkflowApplication 1

1.2 Basic principles for designing WF programs 3

1.2.1 Step 1: Use WPF to create a host for running the process 4

1.2.2 Step 2: Create a Xaml workflow 5

1.2.3 Step 3: running process 6

1.3 running environment comparison between WF4.0 and WF3.X (skip this section if you are not familiar with WF3.X) 7

 

 

Two examples in this article

 

Http://files.cnblogs.com/foundation/LoadXamlSample.rar

 

Http://files.cnblogs.com/foundation/WorkflowApplicationSample.rar

Workflow instance WorkflowApplication and basic principles for designing WF programs
Workflow instance WorkflowApplication

An example of creating a "hello wxwinter" screen output workflow in [getting started with WF4.0.]

 

In this example, we started the process as follows:


Class
Program

{


Static
Void Main (string [] args)

{


WorkflowInvoker. Invoke (new
Workflow1 ());

}

}

 

In practical applications, I do not use WorkflowInvoker. Invoke to create a workflow (I will introduce the WorkflowInvoker function in detail in future articles). I will use WorkflowApplication to create a workflow instance,
WorkflowApplication is used to track and manage a single workflow instance. Let's take a look at the structure of WorkflowApplication:

 

Class Name

System. Activities. WorkflowApplication

File

System. Activities. dll

Structure Description

Inherit WorkflowInstance

Is a sealed class

Function Description

Input an Activity object from the constructor to create a workflow instance.

 


 

WorkflowApplication provides many attributes and methods for managing instances. Today we will introduce the Completed attributes.

Action <WorkflowApplicationCompletedEventArgs> Completed {get; set ;}

When the workflow is completed, the System. Action <T> Delegate specified by this attribute is called.

 

Delegate parameter WorkflowApplicationCompletedEventArgs:

ActivityInstanceState CompletionState

IDictionary <string, object> Outputs

Exception TerminationException

Guid InstanceId

IEnumerable <T> GetInstanceExtensions <T> () where T:

 

The following code uses WorkflowApplication to run the process

Class Program

{

Static void Main (string [] args)

{

// WorkflowInvoker. Invoke (new Workflow1 ());

 

// Create an instance

WorkflowApplication instance = new WorkflowApplication (new Workflow1 ());

 

// Method called when the instance is completed

Instance. Completed = new Action <WorkflowApplicationCompletedEventArgs> (workflowCompleted );

 

// Print the instance ID

System. Console. WriteLine (instance. Id );

 

// Start the instance

Instance. Run ();

 

System. Console. Read ();

}

 

Static void workflowCompleted (WorkflowApplicationCompletedEventArgs e)

{

System. Console. WriteLine ("status: {0}", e. CompletionState. ToString ());

System. Console. WriteLine ("instance No.: {0}", e. InstanceId );

}

}

 

Basic principles for designing WF programs

Most of the time, Beginners use the [Workflow Console Application] project template when learning WF.

 

So that after learning for a long time, many activities are also very skilled, but they simply cannot understand how WF can be applied to actual projects.

 

In fact, the [Workflow Console Application] project template is only used for beginners to practice. WF has two ways to create a process:

1. Use a class to create a process. This is the [Workflow Console Application] project template.

2. Procedure for creating strings in XML format

The purpose of WF design is to be flexible, so the process of creating strings in XML format can be reflected in this way. The following is an example to demonstrate the process of creating strings in XML format,

 

 

 

 

 

 

 

Step 1: Use WPF to create a host for running the process

 

Create a [WFHost] WFP project and name the solution [LoadXamlSample]

 

Add a reference to the [System. Activities] class

 

Add a text box named [run] (AcceptsReturn = "True"), add a button named [runButton], and add a [Click] event for [runButton]


Private
Void runButton_Click (object sender, RoutedEventArgs e)

{

System. Text. UTF8Encoding utf8 = new System. Text. UTF8Encoding ();

 


Byte [] bs = utf8.GetBytes (this. xamlTextBox. Text );

 

System. IO. MemoryStream memoryStream = new System. IO. MemoryStream (bs );

 


Activity activity = ActivityXamlServices. Load (memoryStream );

 


WorkflowApplication myInstance = new
WorkflowApplication (activity );

 

MyInstance. Run ();

}

 

To view the Console output, set the project output type to [Console Application].

 

Step 2: Create a Xaml Workflow

Creating a Xaml workflow with a text file is a tough task. Before you can explain the development process designer to everyone, use VS2010 to create a process.

Create a [Activity Library] project named [wxwinterWorkflow]

 

Design the following process:

 

Obtain the xaml string of the process.

 

Step 3: running process

Run the [WFHost] program, paste the obtained xaml string to the text box, and click the [run] button. The output of "hello wxwinter" is displayed on the console.

 

Change <WriteLine Text = "hello wxwiter"/> in the Text box to <WriteLine Text = "hello wxd"/>, and click the [run] button, the output of "hello wxd" is displayed on the console.

 

Running Environment comparison between WF4.0 and WF3.X (skip this section if you are not familiar with WF3.X)

The most obvious change in the running environment of WF3.X and WF4 is that the CreateWorkflow method of WorkflowRuntime is used to create an instance in WF3.X.

In WF4, you do not need to create a [WorkflowRuntime], but directly use [WorkflowInstance (in bate1)] to create one, this is why [WorkflowInstance] was renamed to [WorkflowApplication] In bate2.


Class
Program

{


Static
Void Main (string [] args)

{


WorkflowRuntime workflowRuntime = new
WorkflowRuntime ();


WorkflowInstance instance = workflowRuntime. CreateWorkflow (typeof (wxwinterActivity ));

Instance. Start ();

System. Console. Read ();

}

}


Public
Class
WxwinterActivity: System. Workflow. ComponentModel. Activity

{


Protected
Override System. Workflow. ComponentModel. ActivityExecutionStatus Execute (System. Workflow. ComponentModel. ActivityExecutionContext executionContext)

{

System. Console. WriteLine ("wxd ");


Return
Base. Execute (executionContext );

}

}

 

 

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.