Beginning WF 4.0 translation-Chapter 3 (flowchart workflow)

Source: Internet
Author: User

In this chapter, you will use the flowchart activity to create a workflow. Just like its name, a flow chart activity is like a flow chart, which is connected by a decision tree. With a sequence activity, its subactivities can only be executed from top to bottom. In a flowchart activity, its subactivities can be executed in any order.

 

Create a flowchart Workflow

Create a project and select the workflow console application template, as shown in the Figure3-1.

 

 

Design Flowchart

Drag a flowchart activity to the designer. The initial graph is similar to a Figure3-2. The green circle represents the Start Node of the flowchart. The empty position below is used to add activities.

 

The main difference between a flowchart activity and a sequence activity is how its subactivities are connected. In chapter 1, when you add activities to sequence, they are always executed from top to bottom. You can rearrange these activities to control their order, but they are always arranged vertically, and arrows between activities are automatically generated. In a flowchart activity, you can place any activity. More importantly, you must draw arrows manually. You can draw an arrow to the previous activity.

In this application, you will output a greeting based on the time of the day. First, output a standard greeting "Hello, world !", Drag a writeline activity under a Green Circle. Set displayname to "hello" and text to "Hello, world !".

 

Define connection

Move the mouse over the green circle and you will see four gray links coming around the green circle, as shown in the Figure3-3.

 

 

 

Click a join point, hold your mouse down, drag the mouse to the hello activity until you see the hello activity connecting point, as shown in the Figure3-4.

 

You do not have to select a connection point. As long as you see the connection point, you will know that the object has been selected and the mouse is released. Then the two activities will be connected. Shown in the Figure3-5.

 

You already know how to connect to an activity. Move the mouse to the Source Activity, see the connection point, click one, drag the mouse to the final activity, when you see the final activity also shows the connection point, release the mouse.

 

FlowdeworkflowActivity

Drag a flowdeworkflow activity to the "hello" activity. The flowde activity looks like a yellow diamond. In the Properties window, enter 'condition' as datetime. Now. Hour> = 12. If you move the mouse over the flowdeworkflow activity, you will see the connection point. Shown in the Figure3-6.

 

True on the left and false on the right. The condition attribute is also displayed. Note that there is a yellow triangle in the upper right corner. If you click it, the condition attribute is displayed even if you move your mouse away. You can change the branch names of true and false. In the Properties window, enter morning in the falselabel property and afternoon in the truelabel property. At this time, when you move the mouse over the activity, you should be able to see the morning and afternoon.

First, connect the "hello" activity to the flowdemo-activity (I won't talk about how to connect ). Drag a writeline to the right of the flowdemo-activity and set displayname to morning and text to "good morning ". Then, press the flowdemo-morning connection point and drag the mouse to the "Morning" activity until you see the connection point. At this point, your graph should be shown in the Figure3-7.

 

Tip: flowdeworkflowActivity is not displayedAttribute. However, it can display ConditionAnd edit trueAnd falseBranch to make it more obvious in the graph.

Drag another flowdecision to the left of the first flowdecision. Set condition to datetime. Now. Hour> = 18. Connect the afternoon branch of the first flowdeploy to the new flowdeploy. Set falselabel to afternoon and truelable to evening. Drag two writeline activities to the designer and name them afternoon and evening respectively. Set the text attribute to "Good afternoon" and "good evening" respectively ", connect the Evening Branch of the second flowdeploy to the "Evening" activity and the afternoon branch to the "Afternoon" activity. Your flowchart should be shown in the Figure3-8 now.

 

 

Run the program

Open the program. CS file before running the program, and add the following code at the end of the main () function:

 

Console. writeline ("press enter to exit ");

Console. Readline ();

 

This can prevent the program from exiting. Click F5. Depending on the time, your results are similar to the following:

 

 

Flow Switch

The flowswitch activity is similar to the flowdeworkflow activity, but the flowswitch is not limited to true or false branches. You can have no limit on the branches. It is similar to the switch in C. Shown in the FlowSwitchFigure3-9.

 

 

Add a flowswitchActivity

Drag a flowswitch activity to the bottom of the workflow. A flowswitch activity is a template class (noticed in toolbox <t>). Therefore, you need to specify its data type. Specify int32. Connect "Morning", "Afternoon", and "Evening" to the flowswitch activity. Flowswitch has only one attribute named expression. Expression is used to determine which branch to take. In this project, you will display different greetings according to different seasons. Enter the following code in the expression attribute:

 

CINT (datetime. Now. Month mod 12) + 1)/4)

 

This expression indicates which season is returned based on the current date. If it is 12, 1, or February, the expression returns 0. Similarly, 3, 4, and May return 1. Now you need to create four branches, one representing a season.

 

Tip: When you move the mouse to flowswitchExpressionWill be displayed, Figure3-10. With flowdeworkflowThe activity is the same. You can click a yellow triangle to maintain the expression.Is displayed.

 

 

 

Add flowstepActivity

Every branch of the flowswitch activity is called flowstep. Although there is no flowstep activity in the toolbox, you cannot explicitly add these branches to the workflow, but when you drag a connection line from the flowswitch activity, the system will automatically generate a flowstep for you. Drag five writeline activities to the vicinity of the flowswitch activity. Set their displaynames to winter, spring, summer, autumn, and default respectively. Connect the five connection lines from the flowswitch to the five writeline activities respectively.

Click a connection line. In the Properties window, you will enter the case value based on different conditions. For "Winter", the value of case should be 1; for "Spring", the value of case should be 2 ,.... For the "default" activity, select the isdefaultcase check box to leave the case value empty.

Your workflow graph should be shown in the Figure3-11.

 

Enter their text attributes in each writeline activity, such as "Happy Summer ". The "default" activity will never be executed because you have defined all possibilities. However, if expression is faulty, the "default" activity will play a role. Enter the text attribute of the "default" activity as follows:

 

"Season caluated as:" + CINT (datetime. Now. Month mod 12) + 1)/4). tostring ()

 

Run the program

Click F5 to run the program. Your results are similar:

 

 

ParallelActivity

Before you end the project, let me demonstrate the parallel activity. Parallel activity allows you to define many activity sequences to run in parallel. For this project, each branch displays different information. Their order is not important. Instead, they are executed sequentially, but at the same time.

 

Add parallelActivity

Drag and Drop a parallel activity to the bottom of the workflow. Drag a connection line from each writeline to the parallel activity. Your workflow should be shown in the Figure3-12.

 

 

 

Add Branch

Double-click the parallel activity and drag and drop three writeline activities to it. One writeline displays the date, the other shows the time, and the last shows the day of the week. Enter the following expressions in the text attribute of each writeline:

 

"Time:" + datetime. Now. timeofday. tostring ()

"Date:" + datetime. Now. Date. to1_datestring ()

"Tody is:" + datetime. Now. tostring ("dddd ")

 

The diagram should be shown in the Figure3-13 at this time:

 

 

Tip: ParallelAn activity only allows one activity on one branch. For this project, it can run well. However, if you need to have multiple activities on each branch, you can use sequenceActivity, and then SequenceAdd multiple activities to the activity.

 

The final workflow diagram Figure3-14 is shown below:

 

 

Run the program

Click F5 to run the program. The result is similar to the following:

 

 

There seems to be something wrong: during my translation, I found that it seems a bit wrong when I judge which season it is. I don't know if I understand the problem or the original article is wrong, however, I wrote a demoI find it is really not. So my demoIt is a little different from the original article. If I am wrong, please point it out!

 

Chapter 3 source code download

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.