Step by step WF series (6)-workflow voting simulation project

Source: Internet
Author: User

1. Summary

In thisArticleIn, we will mainly learn about the usage of replicator activities, and then we will pass a simulated voteProgramFamiliarize yourself with the Replicator activity.

In addition, we will try to decouple the workflow from the specific implementation method in this section.

2. Replicator

In the previous article, we learned the IF-else activity and the while activity. We learned the same idea as we learned the basic syntax of the language, we also lack an important language element in the loop-.

However, in this article, Replicator is not actually the same as for. According to my understanding, I prefer to regard replicator as a foreach semantic activity. As for why, I will explain it in the following examples.

In advanced languages, the functions that can be implemented using for and foreach can also be implemented using while, but in WF, Replicator provides much richer events than while. Therefore, in msdn, it is recommended that replicator instead of while be used first if a task needs to repeat the same function for many different entities.

What is repeating the same function for many different entities? Let's take a look at the following small example:

List<Int> List1 =NewList<Int> (); List1.add (1); list1.add (2); list1.add (3 );Foreach(IntIntitemInList1) {dosomething (intitem );}

AboveCodeAnd repeat dosomething with every element in list1, that is, intitem, as an entity.

We will not talk too much about the theory here. After all, we are in the getting-started stage. Let's get familiar with his knowledge in the example.

3. Simulate Voting

We now have this requirement, which is very simple: we construct a small winform project for voting, and then process it on the workflow side, and require that a string be returned, each item is processed as a string. The user must be selected.

Once again, it is very easy to simply implement this function. Here we will mainly introduce the usage of replicator.

The key to learning replicator is to be familiar with the lifecycle of replicator. I write in chronological order:

A. initialized: occurs when the replicatoractivity starts to be executed.

B. childinitialized: occurs when the subactivity instance is initialized.

C. code_excute: occurs when an internal activity occurs. (This is not a replicator. Here, I just want to help the other party understand the entire cycle)

D. childcompleted: occurs when the child activity ends.

E. Completed: occurs when the entire replicatoracticity ends.

Well, we will use the following steps in the following example to accelerate our understanding:

First, let's clarify the workflow of the above program:

A. Input an array.

B. initialize the internal array of the workflow.

C. traverse the entire array and convert the array into a string

D. Add one to the total number when traversing each item.

E. When each item is traversed to the end, a prompt window is provided for the user to deepen our understanding.

F. When the traversal ends, a prompt is displayed to deepen our understanding.

Okay. Let's write the code step by step:

First, drag a replicator and a code activity:

We first declare an array in the workflow to pass the array in the host to the workflow:

PrivateArraylistArray;PublicArraylistArrayl {Set{Array =Value;}}

Then we thought that since we traverse, We need to specify an object we want to traverse. This should be executed during the initialization of the replicator activity:

 
Private voidActivity_initialized (ObjectSender,EventargsE ){This. Replicatoractivity1.initialchilddata = array ;}

Then we need to know what object we traverse this time before each traversal:

Private voidActivity_childinitialized (ObjectSender,ReplicatorchildeventargsE) {vote = E. instancedataAsString;}

The rest of the work is very simple. Let's take a look at the code of the entire project:

Workflow. CS:

 Public sealed partial class  Workflow1 : Sequentialworkflowactivity { Private string Vote; Private int Countall = 0; Private string Result = String . Empty; Private  Arraylist Array; Public  Arraylist Arrayl { Set {Array = Value ;}} Public String Result { Get { Return Result ;}} Public int Countall { Get { Return Countall ;}}Public Workflow1 () {initializecomponent ();} Private void Activity_initialized ( Object Sender, Eventargs E ){ This . Replicatoractivity1.initialchilddata = array ;} Private void Codeactivityappsexecutecode ( Object Sender, Eventargs E) {countall ++ ;} Private void Activity_childinitialized ( Object Sender,Replicatorchildeventargs E) {vote = E. instancedata As  String ; Vote = "You give" + Vote + "Vote" ;} Private void Activity_childcompleted ( Object Sender, Replicatorchildeventargs E) {result = Result + "\ N" + Vote; MessageBox . Show ( "Complete" );// Actually should not } Private void Activity_completed ( Object Sender, Eventargs E ){ MessageBox . Show ( "All complete" ); // Actually should not }}

Form1.cs:

 Public partial class  Form1 : Form { Private  Workflowruntime Workflowruntime;Private  Workflowinstance Instance; Private  Autoresetevent Waithandle; Private string Result = String . Empty; Int Count; Public Form1 () {initializecomponent (); workflowruntime = New  Workflowruntime (); Workflowruntime. startruntime (); waithandle = New  Autoresetevent ( False ); Workflowruntime. workflowcompleted + = Delegate ( Object Sender1, Workflowcompletedeventargs E1) {result = e1.outputparameters [ "Result" ]. Tostring (); Count = Convert . Toint32 (e1.outputparameters [ "Countall" ]); Waithandle. Set ();};} Private void Button#click ( Object Sender, Eventargs E ){ Dictionary < String , Object > DIC = New  Dictionary < String , Object > (); Arraylist List = New  Arraylist (); For ( Int I = 0; I < This . Checkedlistbox1.checkeditems. Count; I ++) {list. Add ( This . Checkedlistbox1.checkeditems [I]. tostring ();} DIC. Add ( "Arrayl" , List); instance = workflowruntime. createworkflow ( Typeof ( Workflow1 ), DIC); instance. Start (); waithandle. waitone (); MessageBox . Show (result ); MessageBox . Show (count. tostring ());}}

The running result is as follows:

4. Project Summary

In actual projects, our workflow should not involve specific operations. We can understand that a workflow is a tool used by a designer and should not be related to our specific implementation.

For example, it is not advisable to bring up a dialog box in the previous project. This workflow may be used for the Web, winform, or even PDA. Once you write MessageBox in the workflow. code such as show () directly limits the reusability of your workflow.

In addition, we will not write the workflow and Host Program in the same project like the previous project, but should:

This requires that we introduce the workflow DLL in the Host Program.

Well, this is what I wrote this time. I hope you can go back and practice more.

Next notice: Learn WF series step by step (7)-improve the login program and replace if-Else

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.