WF Learning Series 4: Introduction to the application template of the sequential workflow Console

Source: Internet
Author: User

Introduction

WFIt has many roles, such as programming models, runtime engines, and toolsets. It makes it easy to create workflow-based applications. Here we mainly analyzeVs2008Application in the lower-Order Workflow ConsoleProgramAnd learn about several common activities.

Ordered workflow Console Application Template

WFTwo types of workflows are available: sequential workflows and state machine workflows. The two workflows areSequentialworkflowactivity, statemachineworkflowactivityDerived from.Vs2008The ordered workflow console application and class library templates are provided. This article mainly introduces the ordered workflow console application templates.

Create a workflow project

The specific steps are omitted. For more information, see references. View the generated WorkflowCodeAs follows:

 

Code
Public Sealed Partial ClassWorkflow1: sequentialworkflowactivity
{
 PublicWorkflow1 ()
{
Initializecomponent ();
}
}

it is easy to see that, the new workflow workflow1 is from sequentialworkflowactivity derived.

OnlyThere is a starting point and no activity. Here we addCode Activity. After adding this activity, we found that this activity encountered an error becauseExecutecodeNo settings. Double-clickCode ActivityAnd add it to the code file.Console. writeline ("Hello, world! ");. OneHello, world!This completes the workflow.
Parse workflow call code (host program code)

Code
Static   Void Main ( String [] ARGs)

{

//Create a workflow runtime engine to provide a configurable runtime environment for workflow Initialization

Using(Workflowruntime= NewWorkflowruntime ())

{

//Prevent workflow threads from exiting when they are not completed.

// after the workflow thread is completed, the set method is used to notify the main thread that the workflow thread has been completed.

autoresetevent waithandle = New autoresetevent ( false ); // autoresetevent indicates that an event has occurred to the thread that is waiting for the notification, and false indicates that the initial state is not aborted

Workflowruntime. workflowcompleted+ = Delegate(ObjectSender, workflowcompletedeventargs E)

{

Waithandle. Set ();//Notifies the main thread that the workflow has been executed.

};

Workflowruntime. workflowterminated+ = Delegate(ObjectSender, workflowterminatedeventargs E)

{

Console. writeline (E. Exception. Message );

Waithandle. Set ();//Notifies the main thread that the workflow has been executed.

};

 

Workflowinstance instance=Workflowruntime. createworkflow (Typeof(Workflowdemo1.workflow1 ));

Instance. Start ();

 

//The main thread is blocked here until the workflow thread sends a notification (waithandle. Set ())

Waithandle. waitone ();

}

}

Note:

1.PassWorkflowruntimeClass provides a configurable runtime environment for workflow Initialization

2.PassAutoreseteventClass, coordinating the main thread and workflow thread.

3.PassWorkflowinstanceClass to create a workflow instance.

Create a ratio Hello, world More advanced workflows

Job: Create a workflow that supports copying all files in one file to another folder.

Ideas:

1. Create a code activity to parse the source folder and destination folder from the host Program;

2. Create a cyclic activity (one by one copy until all files have been copied)

Embed code activities in cyclic activities to copy a single file

Here we need to explain several questions:

Cyclic activity condition settings

InWhile ActivityDuring the activity, the rule adopts declarative rule conditions. Currently, this is the first file. <A total of several files. Generate the following files in the background:Workflow. Rules:

 

Code
< Ruledefinitions Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/workflow" >

<Ruledefinitions. Conditions>

<RuleexpressionconditionName= "Condition 1">

<Ruleexpressioncondition. Expression>

<! --The operator is defined as a minor sign.-->

<Ns0: codebinaryoperatorexpressionOperator= "Lessthan"Xmlns: ns0= "CLR-namespace: system. codedom; Assembly = system, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089">

<! --Value on the left-->

<Ns0: codebinaryoperatorexpression. Left>

<Ns0: codefieldreferenceexpressionFieldname= "Currentfile">

<Ns0: codefieldreferenceexpression. targetobject>

<Ns0: codethisreferenceexpression/>

</Ns0: codefieldreferenceexpression. targetobject>

</Ns0: codefieldreferenceexpression>

</Ns0: codebinaryoperatorexpression. Left>

<! --Value on the right-->

<Ns0: codebinaryoperatorexpression. Right>

<Ns0: codepropertyreferenceexpressionPropertyname= "Totalfiles">

<Ns0: codepropertyreferenceexpression. targetobject>

<Ns0: codethisreferenceexpression/>

</Ns0: codepropertyreferenceexpression. targetobject>

</Ns0: codepropertyreferenceexpression>

</Ns0: codebinaryoperatorexpression. Right>

</Ns0: codebinaryoperatorexpression>

</Ruleexpressioncondition. Expression>

</Ruleexpressioncondition>

</Ruledefinitions. Conditions>

</Ruledefinitions>

 

In the background

System. workflow. Activities. Rules.RuleconditionreferenceRuleconditionreference1 =NewSystem. workflow. Activities. Rules.Ruleconditionreference();

Ruleconditionreference1.conditionname ="Condition 1";

This. Whileactivity1.condition = ruleconditionreference1;

Load to the cyclic activity.

Communication between the host Program and workflow

How to pass parameters to a workflow:

Define member variables in the workflow:

Public String tofolder {Get; set ;}

Public String fromfolder {Get; set ;}

Public int totalfiles {Get; set ;}

 

When the host program initializes the workflow instance, the parameter is passed in

VaR parameters = new dictionary <string, Object> ();

Parameters. Add ("fromfolder", @ "K:" test ");

Parameters. Add ("tofolder", @ "K:" backup ");

Workflowinstance instance =

Workflowruntime. createworkflow (

Typeof (workflowdemo1.workflow1), parameters );

How to obtain parameters from a workflow:

SlaveWorkflowcompletedeventargs ETo obtain the relevant information in the workflow, such:E. outputparameters ["totalfiles"].

References

This translation does not follow the original document. For details, refer

Http://social.msdn.microsoft.com/content/en-us/msft/netframework/wf/learn/Intro-SequentialWorkflows

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.