Keep learning WF (25): cancelling a container (cancellationhandleractivity)

Source: Internet
Author: User

[Stick to the top] Stick to learning WF Article Indexes

The cancellationhandleractivity activity contains the cleaning logic for the canceled compound activity before all the child activities of the composite activity are completed. In WF, listenactivity, conditionedactivitygroup, and parallelactivity are common activities that can be executed simultaneously. Before all sub-activities are executed, a specific condition (such as a message arriving) can cause the entire activity to be closed immediately. Then, the parent activity cancels all unfinished child activities and calls its corresponding cancellationhandleractivity to execute the clearing logic defined in it.

1. We will take parallelactivity as an example to illustrate how to use this activity. We will create a new ordered workflow project. The workflow design is as follows:

The parallelactivity activity has two branches. When throwactivity is used in the second branch to throw an exception, the entire workflow is terminated. At this time, the first branch of parallelactivity will cancel the operation. Cancel processing in sequenceactivity1 ActivityProgramAdd a codecancel activity to compile our own logic processing program,CodeAs follows:

 
Private voidCodecancel_executecode (ObjectSender,EventargsE ){Console. Writeline ("The activity has been canceled");}
2. The while condition in sequenceactivity1 is true. The code of the entire workflow is as follows:
     Public sealed partial class  Excancelworkflow : Sequentialworkflowactivity { Public Excancelworkflow () {initializecomponent ();} Private void Codecancel_executecode ( Object Sender, Eventargs E ){ Console . Writeline ( "The activity has been canceled" );} Private void Whilecon ( Object Sender, Conditionaleventargs E) {e. Result = True ;} Private void Codeleft_executecode ( Object Sender, Eventargs E ){ Console . Writeline ( "The activity on the left is executed" );} Private void Coderight_executecode ( Object Sender, Eventargs E ){ Console . Writeline ( "The activity on the right is executed" );}}
 
3. The host program code is as follows:
     Class  Program { Static void Main ( String [] ARGs ){ Using ( Workflowruntime Workflowruntime = New  Workflowruntime ()){ Autoresetevent Waithandle =New  Autoresetevent ( False ); Workflowruntime. workflowcompleted + = Delegate ( Object Sender, Workflowcompletedeventargs E) {waithandle. Set () ;}; workflowruntime. workflowterminated + = Delegate ( Object Sender, Workflowterminatedeventargs E ){ Console . Writeline ( "Workflow termination :" + E. Exception. Message); waithandle. Set ();}; Console . Writeline ( "--- Start workflow execution ---" ); Workflowinstance Instance = workflowruntime. createworkflow ( Typeof (Caryexceptiondemo. Excancelworkflow ); Instance. Start (); waithandle. waitone (); Console . Writeline ( "--- The execution of the first workflow ends ---" ); Console . Writeline () ;}}4. The execution result is as follows:
 

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.