[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: