Asynchronous jobs

22.10.2024

What is needed to create a job dispatcher of asynchronous tasks? Not much! Read this article to get an example of a class that runs several tasks asynchronously and stores the result in a table.

Since proof is in the pudding, it's always nice to see how something works and then pull it apart to understand all the various components. This example does not contain much, only a class and a table. 

Create a new table

This table should be called TstTable and contain one string field; ResultString and have Created date time activated. Download the tstTable.xml and put it into the model of your choice.

Create a new class

Download the tstRunAsyncClass.xml and put it into the same model as the table. The class contains code that produces 20 tasks that run asynchronously. Each task has a number, given by the order of creation, and will insert a record in the tstTable with the text "Async task #" + the number of the task. Since createdDateTime is activated on the table, we get a timestamp for each task.

After a build and a sync of the project/model it's time to run the class. And as soon as the class 'tstRunAsyncClass' has completed, you can view the data in the table using the SysTableBrowser.

mi=sysclassrunner&cls=tstRunAsyncClass
mi=SysTableBrowser&TableName=tstTable

Since there isn't a custom index set, the data is sorted by the default RecId column, reflecting the order in which it was inserted. As a result, the task numbers are not in any specific order. Running the class multiple times will produce different task orders.

ResultStringcreatedDateTimeRecId
Async task # 322.10.2024 21:02:175637144576
Async task # 022.10.2024 21:02:175637144577
Async task # 222.10.2024 21:02:175637144578
Async task # 722.10.2024 21:02:175637144579
Async task # 622.10.2024 21:02:175637144580
Async task # 522.10.2024 21:02:185637144581
Async task # 422.10.2024 21:02:185637144582
Async task # 122.10.2024 21:02:185637144583
Async task # 822.10.2024 21:02:185637144584
Async task # 922.10.2024 21:02:185637144585
Async task # 1122.10.2024 21:02:185637144586
Async task # 1222.10.2024 21:02:185637144587
Async task # 1322.10.2024 21:02:185637144588
Async task # 1522.10.2024 21:02:195637144589
Async task # 1422.10.2024 21:02:195637144590
Async task # 1022.10.2024 21:02:195637144591
Async task # 1622.10.2024 21:02:195637144592
Async task # 1722.10.2024 21:02:195637144593
Async task # 1822.10.2024 21:02:195637144594
Async task # 1922.10.2024 21:02:205637144595