Wf4.0 beta1 Tour (3): brand new flowchart

Source: Internet
Author: User
Tags visual studio 2010

I:FlowchartIntroduction

Flowchart is a brand new workflow mode introduced in wf4.0. It provides a good compromise between the sequential and statemachine stream control models. Flowchart allowed
You can use a step-by-step method to implement some simple decision-making and conversion functions, but it also allows you to return previous activities in the workflow. For many users, the flowchart is usually more intuitive.
The WF toolbox in Visual Studio 2010 beta1 contains the following flowchart-related activities:

Flowchart is actually a collection of flow nodes. These flow nodes inherit from the flownode class, and each flow node can contain multiple sub-activities. Pair flow in Flowchart
Flowstep, flowdeswitch, flowswitch, and flowstep are the two active lines in flowchart. They both have an action attribute and a next attribute. Action
The next attribute is used to specify the next element to be executed. Flowswitch is similar to the switch statement in C #. flowdese is equivalent to the IF-else function. Flowchart
It is also an activity. We can also embed flowchart activities in flowchart workflows. In this article, we will use these three activities of flowchart to develop a simple guess digital game to get to know wf4.
Adds a new flowchart workflow.

 

Ii. Development of custom activities

Before developing our workflows, we need to prepare several custom activities. We will elaborate on the Development of custom activities.

1. readkey: Read the characters entered in the console and return,CodeAs follows:

 
NamespaceCaryguessgame {Public classReadkey: Codeactivity <Char> {Protected override voidExecute (codeactivitycontext context) {result. Set (context, console. readkey (True). Keychar );}}}

2. readint: Read the input in the console and convert it to an integer. The Code is as follows:

 Namespace Caryguessgame { Public class  Readint : Nativeactivity < Int > {Inargument <String > Bookmarkname; Public Readint (): Base (){} Public Readint ( String Bookmarkname ): This (){ This . Bookmarkname = bookmarkname ;} Public Inargument < String > Bookmarkname { Get { Return this . Bookmarkname ;} Set {Throwifopen (); This . Bookmarkname = Value ;}} Protected override void Execute (activityexecutioncontext context) {context. createnamedbookmark (bookmarkname. Get (context ), New Bookmarkcallback (onreadcomplete ));} Void Onreadcomplete (activityexecutioncontext context, Bookmark bookmark, Object State ){ Int Guess = convert. toint32 (State ); This . Result. Set (context, guess );}}}

3. Prompt. XAML

Right-click the project to add a new project and use the workflowinstance = "activity template,

3. 1. activity parameters:

Name: bookmarkname direction: in argument type: String

Name: Result direction: Out argument type: int32

Name: Text direction: in argument type: String

3. 2. Drag a sequence activity to prompt. XAML, drag a writeline activity to the sequence activity, and bind the text attribute of the writeline activity to the text parameter.

Drag the readint we just developed under the writeline activity and bind the bookmarkname and result attributes of the readint to the bookmarkname and result parameters.

This completes the activity, for example:

Iii. WorkFlow Design

1. workflow variables and parameters

Variable: Name: Choice type: Char scope: Flowchart

Parameter: Name: maxnumber direction in type: int32
Name: Turns direction out type: int32

2. WorkFlow Design

Below is the workflow design. We will use the flowswitch activity to let users make different choices. A is the game description, B is the start game, and C is the exit. When selecting a, there will be instructions about the game.
Then, we will roll back the process to the user selection section to allow the user to select the relevant functions. Select C to end the workflow. Select B to start the game, that is, the part of the flowchart activity. This part mainly describes
Use of flowswitch activity. See the following figure for details:

In the above flowchart, a branch of the flowswitch activity contains a flowchart activity, which is the logical part of the guess digital game. The following variables are only in the flowchart
Visible in scope:

Variable: Name: Target type: int32 scope: Flowchart
Name: Guess type: int32 scope: Flowchart

The design in flowchart is as follows:

In this figure, we can guess the logic of the number. Here there are two flowdevs. The first is to determine whether the guess is correct. If the guess is correct, the process is over, if you have guessed it wrong, you will be prompted to guess it.
It is still small. Let the process go back and the user guesses again.

4. HostProgram

 Class  Program { Static void Main ( String [] ARGs) {autoresetevent syncevent = New Autoresetevent ( False ); VaR Wfparams = New Dictionary < String , Object > (){{ "Maxnumber" , 50 }}; workflowinstance myinstance = New Workflowinstance ( New Flowchart1 (), wfparams); myinstance. oncompleted = Delegate (Workflowcompletedeventargs e ){ Int Turns = convert. toint32 (E. Outputs [ "Turns" ]); If (Turns! = 0) {console. writeline ( "Congratulations, you guessed it. Count: {0 }" , Turns) ;}syncevent. Set () ;}; myinstance. onaborted = Delegate (Workflowabortedeventargs e) {console. writeline (E. Reason); syncevent. Set () ;}; myinstance. onunhandledexception = Delegate (Workflowunhandledexceptioneventargs e) {console. writeline (E. unhandledexception. tostring ()); Return Unhandledexceptionaction. Terminate;}; myinstance. Run (); // Syncevent. waitone ();  While (! Syncevent. waitone (10, False )){ If (Myinstance. getallbookmarks (). Count> 0 ){ Bool Validentry =False ; While (! Validentry ){ Int Guess; If (! Int32.tryparse (console. Readline (), Out Guess) {console. writeline ( "Please enter an integer ." );} Else {Validentry = True ; Myinstance. resumebookmark ( "Enterguess" , Guess );}}}}}}
 
 

5. The running result is as follows:

Vi. RelatedArticle
Wf4.0 beta1 Tour (1): Basic Introduction
Wf4.0 beta1 Tour (2): Exception Handling

Download Code: caryguessgame.rar

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.