Business Central page background tasks
“The 2019 wave 2 release introduces a way for AL developers to start programming using multithreading/asynchronous concepts. For the first time, developers can now calculate expensive operations in a page without blocking the UI, and then update the UI once the calculation is complete. Attend this session to explore these new programming features.”
Feature details
A page background task can run a codeunit (without a UI) in a read-only child session of the page session. On completion of the task, a completion trigger with the result is invoked on the page session. If the page is closed before the task completes or the page record ID changed, the task is canceled.
Links
Background Tasks
EnqueueBackgroundTask
“EnqueueBackgroundTask” Method
Creates and queues a background task that runs the specified codeunit (without a UI) in a read-only child session of the page session.
If the task completes successfully, the OnPageBackgroundTaskCompleted trigger is invoked. If an error occurs, the OnPageBackgroundTaskError trigger is invoked. If the page is closed before the task completes, or the page record ID on the task changed, the task is cancelled.
Invoking the “EnqueueBackgroundTask” method
Page.EnqueueBackgroundTask(var TaskId: Integer, CodeunitId: Integer [, var Parameters: Dictionary of [Text, Text]] [, Timeout: Integer] [, ErrorLevel: PageBackgroundTaskErrorLevel])
Microsoft Demo Case
The following code extends the Customer Card page with a page background task by using the ENQUEUEBACKGROUNDTASK method.
pageextension 50100 CustomerCardExt extends “Customer Card”
{
layout
{
addlast(General)
{
field(Before1; before1)
{
ApplicationArea = All;
Caption = ‘Before 1’;
Editable = false;
}
field(Duration1; duration1)
{
ApplicationArea = All;
Caption = ‘Duration 1’;
Editable = false;
}
field(After1; after1)
{
ApplicationArea = All;
Caption = ‘After 1’;
Editable = false;
}
}
}
var
// Global variable used for the TaskID
WaitTaskId: Integer;
// Variables for the three fields on the page
before1: Text;
duration1: Text;
after1: Text;
trigger OnAfterGetRecord();
var
//Defines a variable for passing parameters to the background task
TaskParameters: Dictionary of [Text, Text];
begin
// Adds a key-value pair to the parameters dictionary
TaskParameters.Add(‘Wait’, ‘1000’);
//Enqueues the page background task
CurrPage.EnqueueBackgroundTask(WaitTaskId, 50100, TaskParameters, 10000, PageBackgroundTaskErrorLevel::Warning);
end;
Others examples of utilization with demo
I found a very interesting explanation for these topics with related demo on Tobias Fester site, below the link.
Asynchronous programming in D365 Business Central 2019 wave 2: Page Background Tasks
https://github.com/tfenster/pbt-sample
Introducing Async C# – nice reading
https://channel9.msdn.com/Blogs/Charles/Anders-Hejlsberg-Introducing-Async
NEXT SCHEDULED SESSION @DIRECTIONS EMEA
A session about Multithreading/Asynchronous Capabilities topics will be presented at Directions EMEA, Great!
“Introducing Multithreading/Asynchronous Capabilities to AL”
https://www.directionsemea.com/schedule/session-details?tid=MSBK047
Stay Tuned!