{"id":299,"date":"2020-05-19T07:39:39","date_gmt":"2020-05-19T07:39:39","guid":{"rendered":"http:\/\/fredborg.org\/?p=299"},"modified":"2020-12-23T20:45:48","modified_gmt":"2020-12-23T20:45:48","slug":"page-background-task-in-business-central","status":"publish","type":"post","link":"https:\/\/fredborg.org\/?p=299","title":{"rendered":"Page Background Task in Business Central"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This post is a supplement to my video from last week, so if you watched my video, you won&#8217;t find much new in this post, but I know that there are some of you that prefer the written word so here we go <a href=\"https:\/\/emojipedia.org\/beaming-face-with-smiling-eyes\/\" rel=\"noreferrer noopener\" target=\"_blank\">&#x1f601;<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the biggest problems at the moment in Business Central is performance, and one of the ways that you would improve performance in other applications is by making your applications multi-threaded. However this has not really been an option in NAV because NAV is not multi-threaded, now I know one of the ways that you can create a sort of multi-threading in NAV is to offload tasks to the job ques, and while this works it is a complicated way of increasing performance. But have no fear Microsoft has finally created a way of doing multi-threading sort of <a href=\"https:\/\/emojipedia.org\/upside-down-face\/\" rel=\"noreferrer noopener\" target=\"_blank\">&#x1f643;<\/a>. The reason why I say sort of is because, if you expect that Business Central now is 100% multi-threaded, you will sadly be disappointed, because there are some limitations to what you can do, which are the following.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You can only have 5 background tasks running at any given time, if you try to start more they will be queued.<\/li><li>You can only perform read operations.<\/li><li>The background task is tired to a page, which means if you close the page that started the task, then the task will be canceled.<\/li><li>If the record ID is changed the task will be canceled.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Besides these limitations page background tasks great <a href=\"https:\/\/emojipedia.org\/grinning-face\/\" rel=\"noreferrer noopener\" target=\"_blank\">&#x1f600;<\/a>. So how can I use Page Background Tasks? Well, the idea behind Page Background Tasks is that you can offload long-running tasks from the main thread (the UI Thread) unto another thread, thereby not locking the user interface, which in turn means that the user can still use the system without having to wait. So let us take a look at some code <a href=\"https:\/\/emojipedia.org\/nerd-face\/\" rel=\"noreferrer noopener\" target=\"_blank\">&#x1f913;<\/a> you can find all the source code here. <a href=\"https:\/\/dfredborg.visualstudio.com\/Business%20Central\/_git\/Business%20Central?version=GBPageBackgroundTasks&amp;path=%2FMyPageBackgroundTask.cod.al\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/dfredborg.visualstudio.com\/Business%20Central\/_git\/Business%20Central?version=GBPageBackgroundTasks&amp;path=%2FMyPageBackgroundTask.cod.al<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of you need to create a very simple codeuint.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ncodeunit 50100 MyPageBackgroundTask\n{\n    trigger OnRun()\n    var\n        Result: Dictionary of &#x5B;Text, Text];\n        SalesLine: Record &quot;Sales Line&quot;;\n        Total: Decimal;\n    begin\n        \/\/ Run some code\n        if SalesLine.FindSet() then\n            repeat\n                Total := Total + SalesLine.Amount;\n            until SalesLine.Next() = 0;\n\n        Result.Add(&#039;Total&#039;, format(Total));\n\n        Page.SetBackgroundTaskResult(Result);\n    end;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">So in this codeuint I have created a variable of type Dictionary, which is the way that you can communicate between your page and your codeunit, here it is important that your Dictionary is Text, Text because this is the only structure that is supported by Page Background Tasks. Once you have written your logic and added it to your Dictionary, you must call Page.SetBackgroundTaskResult passing your Dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we will create our page which could look as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\npageextension 50100 SalesOrderExt extends &quot;Sales Order&quot;\n{\n    layout\n    {\n        addlast(General)\n        {\n            field(Total; Total)\n            {\n                Editable = false;\n                ApplicationArea = all;\n            }\n        }\n    }\n\n\n    var\n        Total: Text;\n        WaitTaskId: Integer;\n\n\n    trigger OnAfterGetRecord()\n    var\n        \/\/Defines a variable for passing parameters to the background task\n        TaskParameters: Dictionary of &#x5B;Text, Text];\n    begin\n        TaskParameters.Add(&#039;Wait&#039;, &#039;1000&#039;);\n        \/\/TaskID, CodeunitId, Parameters, Timeout, ErrorLevel\n        CurrPage.EnqueueBackgroundTask(WaitTaskId, 50100, TaskParameters, 1000, PageBackgroundTaskErrorLevel::Error);\n    end;\n\n\n    trigger OnPageBackgroundTaskCompleted(TaskId: Integer; Results: Dictionary of &#x5B;Text, Text])\n    begin\n        if (TaskId = WaitTaskId) then begin\n            Total := Results.Get(&#039;Total&#039;);\n        end;\n    end;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here you must create a global Integer I have called mine WaitTaskId, you do not need to assign any value to this Integer, but you must have it, since it holds your task ID, on the OnAfterGetRecord trigger you must declare a Dictionary which you can assign some values that you wish to pass to the task codeunit. Next, you must call CurrPage.EnqueueBackgroundTask where you define your TaskID the codeunit that you wish to execute, your parameters, a timeout if you wish to avoid your task running for too long and the error level, at which level you wish your task to fail. Next, you must define a trigger called OnPageBackgroundTaskCompleted in which you handle the result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And that is all there is to background tasks, now like a wrote earlier it is not 100% multi-threading but it is a step in the right direction and I am looking forward to seeing if we will see more, however, I do not believe that it is going to happen any time soon, because one of the biggest problems with implementing multi-threading in Business Central is that is so tightly coupled with the database, which makes it nearly impossible to allow write transactions in background tasks, which means Microsoft would have to rewrite the core of Business Central to be Object Orientated <a rel=\"noreferrer noopener\" href=\"https:\/\/emojipedia.org\/thinking-face\/\" target=\"_blank\">&#x1f914;<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is a supplement to my video from last week, so if you watched my video, you won&#8217;t find&#8230; <a class=\"read-more\" href=\"https:\/\/fredborg.org\/?p=299\" rel=\"nofollow\"> Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":300,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,1],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-business-central","category-uncategorized","shapla-grid-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Page Background Task in Business Central - Fredborg<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fredborg.org\/?p=299\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Page Background Task in Business Central - Fredborg\" \/>\n<meta property=\"og:description\" content=\"This post is a supplement to my video from last week, so if you watched my video, you won&#8217;t find... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fredborg.org\/?p=299\" \/>\n<meta property=\"og:site_name\" content=\"Fredborg\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-19T07:39:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-23T20:45:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1077\" \/>\n\t<meta property=\"og:image:height\" content=\"472\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"dfredborg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dfredborg\" \/>\n<meta name=\"twitter:site\" content=\"@dfredborg\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"dfredborg\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fredborg.org\/?p=299#article\",\"isPartOf\":{\"@id\":\"https:\/\/fredborg.org\/?p=299\"},\"author\":{\"name\":\"dfredborg\",\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"headline\":\"Page Background Task in Business Central\",\"datePublished\":\"2020-05-19T07:39:39+00:00\",\"dateModified\":\"2020-12-23T20:45:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fredborg.org\/?p=299\"},\"wordCount\":663,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"image\":{\"@id\":\"https:\/\/fredborg.org\/?p=299#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png\",\"articleSection\":[\"Business Central\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/fredborg.org\/?p=299#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fredborg.org\/?p=299\",\"url\":\"https:\/\/fredborg.org\/?p=299\",\"name\":\"Page Background Task in Business Central - Fredborg\",\"isPartOf\":{\"@id\":\"https:\/\/fredborg.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fredborg.org\/?p=299#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fredborg.org\/?p=299#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png\",\"datePublished\":\"2020-05-19T07:39:39+00:00\",\"dateModified\":\"2020-12-23T20:45:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fredborg.org\/?p=299#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fredborg.org\/?p=299\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fredborg.org\/?p=299#primaryimage\",\"url\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png\",\"contentUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png\",\"width\":1077,\"height\":472},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fredborg.org\/?p=299#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fredborg.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Page Background Task in Business Central\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fredborg.org\/#website\",\"url\":\"https:\/\/fredborg.org\/\",\"name\":\"Fredborg\",\"description\":\"Business Central and Azure\",\"publisher\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fredborg.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\",\"name\":\"dfredborg\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png\",\"contentUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png\",\"width\":256,\"height\":256,\"caption\":\"dfredborg\"},\"logo\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:fredborg.org\",\"https:\/\/www.linkedin.com\/in\/dfredborg\/\",\"https:\/\/x.com\/dfredborg\",\"https:\/\/www.youtube.com\/channel\/UCUNZglLDMjBlOK_T9JtI8UA\"],\"url\":\"https:\/\/fredborg.org\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Page Background Task in Business Central - Fredborg","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fredborg.org\/?p=299","og_locale":"en_US","og_type":"article","og_title":"Page Background Task in Business Central - Fredborg","og_description":"This post is a supplement to my video from last week, so if you watched my video, you won&#8217;t find... Read more","og_url":"https:\/\/fredborg.org\/?p=299","og_site_name":"Fredborg","article_published_time":"2020-05-19T07:39:39+00:00","article_modified_time":"2020-12-23T20:45:48+00:00","og_image":[{"width":1077,"height":472,"url":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png","type":"image\/png"}],"author":"dfredborg","twitter_card":"summary_large_image","twitter_creator":"@dfredborg","twitter_site":"@dfredborg","twitter_misc":{"Written by":"dfredborg","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fredborg.org\/?p=299#article","isPartOf":{"@id":"https:\/\/fredborg.org\/?p=299"},"author":{"name":"dfredborg","@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"headline":"Page Background Task in Business Central","datePublished":"2020-05-19T07:39:39+00:00","dateModified":"2020-12-23T20:45:48+00:00","mainEntityOfPage":{"@id":"https:\/\/fredborg.org\/?p=299"},"wordCount":663,"commentCount":0,"publisher":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"image":{"@id":"https:\/\/fredborg.org\/?p=299#primaryimage"},"thumbnailUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png","articleSection":["Business Central"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fredborg.org\/?p=299#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fredborg.org\/?p=299","url":"https:\/\/fredborg.org\/?p=299","name":"Page Background Task in Business Central - Fredborg","isPartOf":{"@id":"https:\/\/fredborg.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fredborg.org\/?p=299#primaryimage"},"image":{"@id":"https:\/\/fredborg.org\/?p=299#primaryimage"},"thumbnailUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png","datePublished":"2020-05-19T07:39:39+00:00","dateModified":"2020-12-23T20:45:48+00:00","breadcrumb":{"@id":"https:\/\/fredborg.org\/?p=299#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fredborg.org\/?p=299"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fredborg.org\/?p=299#primaryimage","url":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png","contentUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/05\/BackgroundTask.png","width":1077,"height":472},{"@type":"BreadcrumbList","@id":"https:\/\/fredborg.org\/?p=299#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fredborg.org\/"},{"@type":"ListItem","position":2,"name":"Page Background Task in Business Central"}]},{"@type":"WebSite","@id":"https:\/\/fredborg.org\/#website","url":"https:\/\/fredborg.org\/","name":"Fredborg","description":"Business Central and Azure","publisher":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fredborg.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d","name":"dfredborg","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fredborg.org\/#\/schema\/person\/image\/","url":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png","contentUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png","width":256,"height":256,"caption":"dfredborg"},"logo":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/image\/"},"sameAs":["https:fredborg.org","https:\/\/www.linkedin.com\/in\/dfredborg\/","https:\/\/x.com\/dfredborg","https:\/\/www.youtube.com\/channel\/UCUNZglLDMjBlOK_T9JtI8UA"],"url":"https:\/\/fredborg.org\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299"}],"version-history":[{"count":1,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions\/301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/media\/300"}],"wp:attachment":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}