One step to learn WF series (1) -- start with Hello World

Source: Internet
Author: User

1. What is a workflow?

I have defined a workflow as a process that produces certain results.

In fact, in the OA system, the approval flow is one of our most common workflows.

In addition, simply put, the flow chart we draw is actually the simplest workflow, and the biggest feature of the workflow is the graphics. A workflow triggers the next operation in sequence or event-driven mode to get a final result.

In fact, we can also regard it as a procedural responsibility chain.

2. workflow type

There are two types of workflows: sequential workflows and event-driven workflows.

A sequential workflow is a common workflow. It is a step by step, and the steps they perform cannot be changed.

An event-driven workflow is also called a finite state machine. Its state changes are triggered by specific events.

Example:

Sequential workflow: The approval flow is a typical sequential workflow. An approval object is approved step by step. In the interview process, the Personnel Department will first screen the resume and then the technical manager will interview, then the General Manager interview, which is based on the process.

Event-driven workflow: Consider whether our software engineering process is like this:

3. First Instance: Hello World

Learn anyProgramThe first example is usually "Hello World", which is no exception this time.

Let's first build a simple WF applet.

The environment I use is Visual Studio 2008 +. NET Framework 3.5 SP1 to build a project:

First, let's get familiar with the environment:

Open vs2008 and choose new --> Project:

Then select workflow --> ordered workflow console application. Click OK to go to our WF project.

In the project, we can see program. CS and workflow1.cs. Workflow1: I don't know much about it. Of course, this is our workflow program.

Let's take a look at program. CS:

4. Host

Windows Workflow Foundation is not an independent product. It can only run in a host environment.

This host can be a console application, a winform program, or an ASP. NET program.

WF is implemented through the runtime engine of the workflow. In fact, the workflow is running in the same process as the host application.

5. Continue Hello World

Drag a code control in the toolbox.

Then a meaningful name for codeactivity1 is codeactivityhello.

Then implement the excutecode event to implement this event, and the runtime will automatically call this method.

The implementation is as follows:

Private voidCodeactivityhello_executecode (ObjectSender,EventargsE ){Console. Writeline ("Hello World");}

Okay, let's run this program:

6. parse the host file in Depth

Let's further parse the host file program. CS:

 Static void Main ( String [] ARGs ){ Using ( Workflowruntime Workflowruntime = New  Workflowruntime ()){Autoresetevent Waithandle = New  Autoresetevent ( False ); Workflowruntime. workflowcompleted + = Delegate ( Object Sender, Workflowcompletedeventargs E) {waithandle. Set () ;}; workflowruntime. workflowterminated + = Delegate ( Object Sender, Workflowterminatedeventargs E ){ Console . Writeline (E. Exception. Message); waithandle. Set ();}; Workflowinstance Instance = workflowruntime. createworkflow ( Typeof (Workflowconsoleapplication3. Workflow1 ); Instance. Start (); waithandle. waitone ();}}

Workflowruntime: provides an executable environment for the workflow execution engine.

Next, the workflowruntime. workflowcompleted event and workflowruntimeterminated specify the anonymous methods called at the end of the workflow execution and the end of the execution respectively.

Next, create an instance for the corresponding workflow and run the instance.

Then call the instance waithandle of autoresetevent. waitone () is used to stop the execution of the current thread, so that after the workflow is completed, that is, workflowruntime. after workflowcomplete event technology, we can call and execute other statements in this statement.

There is nothing to say. Next, create a workflow instance and start execution.

7. Make the program more interesting

After analyzing the host file, we began to make the program more interesting.

Let's start with the two anonymous methods.

First, modify the backend of workflow1.cs.Code:

 
Public sealed partial classWorkflow1:Sequentialworkflowactivity{PublicWorkflow1 () {initializecomponent ();}Private stringMessage;Public StringMessage {Get{ReturnMessage ;}}Private voidCodeactivityhello_executecode (ObjectSender,EventargsE ){This. Message ="Hello World";}}

When a workflow is executed, it assigns the message attribute to the object instantiated by workflow1 as Hello world. What we need to do is to obtain this message.

Let's take a look at the essence. In fact, in the workflow object, all attributes are stored in the hash table in the form of key-value pairs. Therefore, you can obtain the corresponding attributes through the workflowcompleteeventargs parameter. The Code is as follows:

 
StringMessage =String. Empty; workflowruntime. workflowcompleted + =Delegate(ObjectSender,WorkflowcompletedeventargsE) {message = E. outputparameters ["Message"]. Tostring (); waithandle. Set ();};

Next, we can print the message.

 
WorkflowinstanceInstance = workflowruntime. createworkflow (Typeof(Workflowconsoleapplication3.Workflow1); Instance. Start (); waithandle. waitone ();Console. Writeline (Message );

8. Further transformation procedures

Since we can obtain parameters, we can also input parameters to the workflow.

Let's first rebuild the workflow1.cs background code file:

 Public sealed partial class  Workflow1 : Sequentialworkflowactivity { Public Workflow1 () {initializecomponent ();} Private string Message; Public String Message { Get { Return Message ;}} Private string Input;Public String Input { Set {Input = Value ;}} Private void Codeactivityhello_executecode ( Object Sender, Eventargs E ){ This . Message = "Hello" + Input ;}}

Passing parameters to a workflow is actually very simple. Let's take note of this method:

 
WorkflowinstanceInstance = workflowruntime. createworkflow (Typeof(Workflowconsoleapplication3.Workflow1));

This method instantiates a corresponding workflow instance by passing in a type. Of course, we use the overload method of this method to pass in the corresponding instance:

Console. Writeline ("Please input your name :");StringInput =Console. Readline ();Dictionary<String,Object> DIC =NewDictionary<String,Object> (); DIC. Add ("Input", Input );WorkflowinstanceInstance = workflowruntime. createworkflow (Typeof(Workflowconsoleapplication3.Workflow1), DIC); instance. Start (); waithandle. waitone ();Console. Writeline (Message );

 

See the results:

9. Summary

The preceding is the simplest example of a workflow.

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.