Interrupting parallel tasks in a process

This article offers examples of implementation of parallel tasks, with a possibility to interrupt one of the flows.

In some processes, different activities are executed at the same time. For example, consider a business trip, where all the documents are prepared at the same time as the travel allowance is being paid out. It is important to take into account the possibility to cancel the payment and, therefore, stop the preparation of documents. 

 

Attention!

Sometimes you need to interrupt similar parallel tasks when one of them is completed. Usually, this is implemented by means of the “Who was first” concept. You can read more about it in this article.

 

Interrupting one of the parallel flows

Consider the example with the travel allowance, which is given out at the same time as the travel documents are prepared.

 

If the allowance is denied, the “Interrupt preparation” script is executed. It stops the currently active task and attributes the True value to the Interrupt Task context variable.

 

1 public void AbortTask(Context context)
2        {
3             
4            if (context.TaskToAbort!=null)
5            {
6                // Get element of the process map
7                var element = 8(BPMNFlowElement)context.TaskToAbort.WorkflowBookmark.Instance.Process.Diagram.Elements.Si9 ngle(e => e.Uid == context.TaskToAbort.WorkflowBookmark.ElementUid);
10                // Search for an outgoing transition by its name. Since there are two 
11 tasks, search for one of the two corresponding transitions.
12                var connector = element.OutputConnectors.FirstOrDefault(c => (c.Name ==
13 "Next"||c.Name=="Ready"));
14                if (connector != null)
15               {
16                   // Transition found
17                  // Form data for task execution
18                    var executeData = new WorkflowTaskExecuteData(context.TaskToAbort, connector.Uid);
19                    // Execute task
20 Locator.GetServiceNotNull<IWorkflowRuntimeService>21().Execute(executeData);
21 context.PrepvatjZadachi = true;
22                }
23            }
24        }


For the script to work, connect the following namespaces:

 

1 using EleWise.ELMA.Tasks.Models;
2 using EleWise.ELMA.Workflow.BPMN.Diagrams.Elements;
3 using EleWise.ELMA.Workflow.Models;
4 using EleWise.ELMA.Workflow.Services;
5 using EleWise.ELMA.Services

 

To write the “Issue business travel form” or the “Book tickets” task to the TaskToAbort context variable (Type – base task class), you can use the following script. 

1 public override void OnTaskCreate(ITaskBase task, 
2 EleWise.ELMA.Model.Entities.ProcessContext.PMyPrcoessName context)
3      {
4          //select the required task by operation name
5          if ((task.Subject == "Book tickets")|(task.Subject == "Issue business travel form"))
6          {
7              //write the task to the context variable
8              context.TaskToAbort=(TaskBase)task;
9          }
10      }

 

If you do not want the tasks that follow the interrupted task to be executed, use the conditional operationread more about it here

 

The condition looks as follows:

1 Interrupt tasks = false

 

 

There is an easier way to stop a task: escalation by script.

 

 

The script checks and returns the value of the yes\no “Interrupt Task” variable The text of the script is as follows:

 

1 public bool CheckMoneyDenied(Context context)
2      {
3           return context.PrepvatjZadachi;
4       }

 

In escalations settings, set up the periodicity. For example, if you specify 1 minute, then the script will be executed once in a minute. If at that moment Interrupt Task = TRUE, the task is interrupted.

This method, however, has one disadvantage: the task is not interrupted immediately, but only when the script is executed according to the timer.