One-step learning of WF series (5)-workflow simulation game

Source: Internet
Author: User

1. Summary

In the above article, we used ifelse to simulate a login process.

In this article, we will use while to simulate a guessing game.

In recent articlesArticleI will have some simulated small examples to explain the usage of the specific active control, but it is just a small example. In actual engineering, we all use WF to build an overall macro framework process. This is not a small example and we hope you will be familiar with it.

However, as the question says, let's learn WF step by step. Start from scratch.

2. While

Just like his name, he represents our while semantics in general languages.

As long as its condition is true, the while activity will be executed cyclically.

3. Guess the number of games

Create a new console application.Program.

The game then generates a random number ranging from 1 to 10 at random, and then asks the user to input the random number, and finally returns the number of user guesses.

First, let's drag a while control and two code controls to the design interface.

In the workflow backgroundCodeTo declare a result attribute and specify its set attribute. In this way, the client can dynamically input this number, which can be randomly generated or read from the database, read from the file.

Remember, we have to do our best to separate the workflow from the specific implementation details.

Next, we use declarative code conditions to specify the while activity conditions:

In this way, we will judge whether the input and result are equal each time.

Then, we use outputparemeter to obtain the outgoing parameters in the workflow.

The complete code example is as follows:

Workflow1.cs

 Public sealed partial class  Workflow1 : Sequentialworkflowactivity { Private int Input; Private int Result; Private int Count; Public int Count { Get { Return Count ;}}Public int Result { Set {Result = Value ;}} Public Workflow1 () {initializecomponent ();} Private void Codeactivityappsexcutecode ( Object Sender, Eventargs E ){ Console . Writeline ( "Enter the number you want to judge :" ); String S = Console . Readline (); input =Convert . Toint32 (s); count ++ ;} Private void Codeactivity2_executecode ( Object Sender, Eventargs E ){ Console . Writeline ( "Successful" );}}

Program. CS:

 Class  Program { Static void Main ( String [] ARGs ){ Using (Workflowruntime Workflowruntime = New  Workflowruntime ()){ Autoresetevent Waithandle = New  Autoresetevent ( False ); Object Count = Null ; Workflowruntime. workflowcompleted + = Delegate ( Object Sender, Workflowcompletedeventargs E) {COUNT = E. outputparameters [ "Count" ]; Waithandle. Set () ;}; workflowruntime. workflowterminated + = Delegate ( Object Sender, Workflowterminatedeventargs E ){ Console . Writeline (E. Exception. Message); waithandle. Set ();}; Random R = New  Random (); Int Result = R. Next (10 );Dictionary < String , Object > DIC = New  Dictionary < String , Object > (); DIC. Add ( "Result" , Result ); Workflowinstance Instance = workflowruntime. createworkflow ( Typeof (Workflowconsoleapplication5. Workflow1 ), DIC); instance. Start (); waithandle. waitone ();Console . Writeline ( "You guessed" + Count. tostring () + "Times" );}}}

4. Prompt

Many of my friends have asked me what we are doing with a workflow? What is the purpose of a workflow? In the previous examples, isn't the code well written?

Therefore, I think it is necessary to reiterate the meaning and purpose of the workflow.

Workflow provides us with a macro framework. We should build a complex business process with a workflow, or they are event-driven, or they are executed in sequence.

Their process is generally not as simple as logging on, guessing, but persistent, or persistent to a disk file, or persistent to a database. These will be mentioned later.

Here, we only need to remember that the workflow solves a macro current affairs process for us.

5. Summary

In this article, we mainly introduce the usage of the while activity and the parameter transfer between the workflow and the Host Program.

I hope you can repeat the code to deepen your memory.

Next, we will announce the step-by-step WF series (6)-workflow voting simulation program.

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.