Cancellation scope's usage

Folks,

Greeting!
who knows how to use cancellation scope and what for?

Thanks,
ray

2 Likes

hi @rzhang

You can go through with this link

Regards…!!

3 Likes

Can you give me a example?
Thanks!

1 Like

Can someone please explain how to use it in UiPath

hey @Priyam and @Aexx_yuki

Let me give a depth about Cancellation Scope with an Example in Transaction Process Sample.

Many middle tier components and services rely on the well-known programming construct of transactions to handle cancellation for them.

However, there are many situations in which work that cannot be done in a transaction must be canceled.
Using cancellation is more difficult than using transactions, because work that must be canceled must first be tracked.
There are many times when you must cancel work that cannot be done under a transaction. Cancellation can be very difficult because you have to track work that has been done to know how to cancel it

By using Cancellation Scope Activity will help you to handle such scenario. :slight_smile:

How does cancellation happen?

Cancellation can be triggered either from within an activity or from the activity’s parent.There are 2 ways it can happen, from inside of a workflow or from the outside. Child workflow activities are scheduled by their parent activity such as a Sequence, Parallel, Flowchart or some custom activity. The parent activity can decide to cancel child activities for any reason. For example, a Parallel activity with three child branches will cancel the remaining child branches whenever it completes a branch and the CompletionCondition expression evaluates to true.

The workflow can also be cancelled from the outside by the host application by calling the Cancel method on the WorkflowInstance.

How does the cancellation scope work?

  1. The cancellation scope has a Body.This is where you do the work that you might have to cancel later.
  2. The CancelHandler contains the activities you want to run if the activity is canceled.

To use the CancellationScope activity, put the work that needs to be canceled into the Body property, and put the work that is performed after cancellation into the CancellationHandler property.

  • An activity can be canceled only if it has not completed. In the case of the CancellationScope activity, completion refers to the completion of the activities in the Body

  • If a cancellation request is scheduled and the activities in the Body have not completed, then the CancellationScope will be marked as Canceled and the CancellationHandler activities will be executed.

Does an unhandled exception cause the cancellation handler to be invoked?

No it does not. Unhandled exceptions cause the workflow to abort, cancellation handlers are not invoked

What if my cancellation handler throws an unhandled exception?

If you have an unhandled exception in the cancellation handler the workflow will immediately terminate with the exception. This is similar to throwing an exception from within a catch block.

Example - Cancelation Scope Example.xaml (10.8 KB)

Explanation of the above Example:slight_smile:

The scenario that the sample uses to demonstrate the CancellationScope activity is a client wanting to buy a ticket to Miami as soon as possible.

  1. There are two travel agencies that want the business.

  2. The sample uses two CancellationScope within a Parallel activity to model this business logic.

  3. The CompletionCondition of the Parallel activity is set to true; since the CompletionCondition is checked after any branch completes, this will cause the remaining branch to be canceled after the first branch completes.

  4. The client application asks both agencies to buy the ticket.

  5. When the first one confirms that the ticket has been bought, the application cancels the order at the other agency.

you will get the following output when you will run the above sample:
image

So it is clearly visible that cancellation scope 2 was able to book the ticket and parallel completions status has been satisfied so it defered again to go back to Cancellation SCope one Cancellation handler part.

Hope that now you guys will be able to understand its usage :slight_smile:

Regards…!!
Aksh

10 Likes

Thanks for explaining the cancellation scope in detail. Are we talking here about the workflow ie: .xaml files in the project? If yes, then could you please explain how to use it? Also apart from Parallel activities where and how can we use this activity?

hey @Priyam

here Host application is A host can cancel a workflow by calling the Cancel method of the WorkflowApplication instance that is hosting the workflow.

Yes we can use depending on your Process need and logic you can use it even with a sequence.

There is one simple explanation when we executes our workflow and something went wrong for an example an exception will occur then the rest steps will get cancel and we will get exception.
so workflow will call cancel on the activities.
Activities can be canceled if an exception will occur.

In the below Example workflow XAML file my workflow consists of a Sequence Activity.The Sequence is specified as the Body of a Cancellation Scope activity which is contained by a Try-Catch activity. An exception is thrown from the body of the Sequence, is handled by the parent Try-Catch Activity, and the Sequence is canceled.

When this workflow is invoked, the following output is displayed to the console.

You can clearly see first cancellation handler fired to handle the Cancellation of a sequence.

Cancellation Scope with Sequence.xaml (8.3 KB)

Regards…!!
Aksh

4 Likes

can be used in good places :slight_smile:
thx

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.