1. Summary
Remember the one in "Step by Step WF series (6)-workflow voting simulation project"Program? In that articleArticleWe used a workflow to simulate a simple voting project.
In this article, we will continue to improve this voting item.
2. Purpose
In the above Article, our voting project is indefinite, but we should consider the actual situation, for example, a vote of the most popular singer, which requires a deadline, for example, the voting period is one month.
In this article, we will set up a deadline for voting.
(Note: The purpose of our article is not to be a project, but to explain how to use the activity. In some cases, the purpose of demonstration is not to actually use the control)
3. Delay Activity
Delay is an event-type activity. As we can see from the activity name, the delay activity means delay, that is, to delay our activity for a certain period of time.
Investigate the nature of delay. delay is to listen asynchronously when the workflow instance is started and then automatically triggered.
Let's take a look at the events related to the delay activity. An important attribute of the delay activity is timeoutduration, which sets a timeout time and automatically triggers the activity after the timeout.
This activity is often used as a supplement to other event types. When a workflow is not triggered by the outside world, it is deemed that the activity times out and the delay time is triggered.
You may be confused when you hear so much about it. I think you will understand what this activity means.
4. eventhandlingscope Activity
Eventhandlingscope has a main activity. Before the main activity is executed, it monitors other activities and executes other activities.
The primary activity is placed in the default view, while other activities are placed in the event processing view.
In addition, the events in the event view are composed of eventdriven containers.
Let's improve our voting project step by step.
5. Project Analysis
Let's assume that the voting project is valid for one minute.
We need to trigger workflow voting events on the application side.
6. Create an object class
We need to create an entity class for voting information. The attributes in the class include guid and the number of votes cast by the client for a certain option.
Namespace Model {[ Serializable ] Public class Voteinfo : Externaldataeventargs {Private Guid Guid; Private int Count; Public Voteinfo ( Guid Guid, Int Count ): Base (Guid ){This . Count = count ;} Public Guid Guid { Get { Return Guid ;} Set {Guid =Value ;}} Public int Count { Get { Return Count ;} Set {COUNT = Value ;}}}}
7. Agreed Interface
We need to specify an interface to allow the application to trigger the relevant time of the workflow when voting. We need to input the number of votes to the workflow.
Namespace Interface {[ Externaldataexchange ] Public interface Ifavorate { Event Eventhandler < Externaldataeventargs > Lovezidane; Event Eventhandler < Externaldataeventargs > Lovebeckham; Event Eventhandler < Externaldataeventargs > Loveronaldo; Event Eventhandler < Externaldataeventargs > Lovekaka ;}}
In actual projects, we certainly won't do this. Here we want to better explain the activity.
8. Complete the Workflow
This is our default view:
Next, we drag four eventdriven containers in the event processing view, drag a handlerexternal event into each container, and let each option correspond to an activity.
Then set the parameters as follows:
Then we set a default number of votes for each star in the workflow. When each event is triggered, we modify the number of votes.
Complete WorkflowCodeAs follows:
Public sealed partial class Workflow1 : Sequentialworkflowactivity { Private int Zidanecount = 0; Private int Beckhamcount = 0; Private int Ronaldocount = 0;Private int Kakacount = 0; Public Workflow1 () {initializecomponent ();} Private void Handleexternaleventactivityincluinvoked ( Object Sender, Externaldataeventargs E ){ Voteinfo Vote = E As Voteinfo ; If (Vote! = Null ) {Zidanecount + = vote. Count ;}} Private void Handleexternaleventactivity2_invoked ( Object Sender, Externaldataeventargs E ){Voteinfo Vote = E As Voteinfo ; If (Vote! = Null ) {Beckhamcount + = vote. Count ;}} Private void Handleexternaleventactivity3_invoked (Object Sender, Externaldataeventargs E ){ Voteinfo Vote = E As Voteinfo ; If (Vote! = Null ) {Ronaldocount + = vote. Count ;}} Private void Handleexternaleventactivity4_invoked ( Object Sender, Externaldataeventargs E ){ Voteinfo Vote = E As Voteinfo ;If (Vote! = Null ) {Kakacount + = vote. Count ;}} Private void Codeactivityappsexecutecode ( Object Sender, Eventargs E ){ If (Zidanecount + beckhamcount + ronaldocount + kakacount = 0 ){ MessageBox . Show ( "No vote" );} Else { String Result = "Zidane has" + Zidanecount +"Ticket \ n" ; Result + = "Beckham has" + Beckhamcount + "Ticket \ n" ; Result + = "Ronaldo has" + Ronaldocount + "Ticket \ n" ; Result + = "Kaka has" + Kakacount + "Ticket \ n" ;MessageBox . Show (result );}}}
9. Complete the client
Let's design a simulated client:
The Code is as follows:
Public partial class Form1 : Form , Ifavorate { Private Workflowruntime Runtime;Private Externaldataexchangeservice Service; Private Workflowinstance Instance; Public Form1 () {initializecomponent (); Workflowruntime Runtime = New Workflowruntime (); Service =New Externaldataexchangeservice (); Runtime. addservice (service); service. addservice ( This ); Runtime. startruntime (); instance = runtime. createworkflow ( Typeof (Workflowconsoleapplication10. Workflow1 ); Instance. Start ();} Private void Button#click ( Object Sender, Eventargs E ){ Int Count; Bool Temp = Int32 . Tryparse ( This . Textbox1.text, Out Count ); If (Temp ){If ( This . Radiobutton1.checked) {lovezidane ( Null , New Voteinfo (Instance. instanceid, count ));}If ( This . Radiobutton2.checked) {lovebeckham ( Null , New Voteinfo (Instance. instanceid, count ));}If ( This . Radiobutton3.checked) {loveronaldo ( Null , New Voteinfo (Instance. instanceid, count ));}If ( This . Radiobutton4.checked) {lovekaka ( Null , New Voteinfo (Instance. instanceid, count ));}}}# Region Ifavorate Member Public event Eventhandler < Externaldataeventargs > Lovezidane; Public event Eventhandler < Externaldataeventargs > Lovebeckham; Public event Eventhandler <Externaldataeventargs > Loveronaldo; Public event Eventhandler < Externaldataeventargs > Lovekaka; # Endregion }
10. Test results:
11. Conclusion
There is nothing to summarize, But I still hope to go back and become more familiar with the exercises.
In addition, I submitted a resignation letter to the company. I am looking for a job. I hope you can help me a lot. Thank you.
Below is the source code of my above project and my resume. If you can, please help your younger brother send a resume to HR.
Thank you.
12. Data download
Project source code: Improves voting items
resume: Resume