|
|
|
|
|
- Overview
- Principle
- How to?
- Principle
- Create and run a parallel task
- MyParallelTask keyword
- Wait for the execution of the task and retrieve the return value
- Managing the sequence of parallel tasks
- Handling the controls from a parallel task
- Example: Accelerating the processes
- Accelerating the processes: statistical calculation
- Example: Improving the reactivity of application
- Improving the reactivity of application
- Updating the GUI via a continuation task
- Updating the GUI via a procedure run in the main thread
The computers are becoming more and more powerful. The computers have powerful processors with several cores. To improve the performance of applications and to work with a maximum number of computer cores, you now have the ability to divide the processes into a list of sub-processes (called tasks) and to run them in parallel rather than sequentially. A task is a procedure to run that can expect parameters and that can return a result. These tasks will be run by the computer in one or more threads according to the computer availability. A task can be divided into several sub-tasks. What is the benefit of parallel tasks? The parallel tasks are useful to: - speed up the process time of application via the parallelism: several processes are run in parallel instead of being run sequentially: the application speed is improved.
A simple example: start a statistical calculation for sending and receiving emails on each email address of the database. If the statistical calculation on an email address takes one second and if the database contains 200 000 email addresses, the calculation takes more than two days. To go faster, you have the ability to start a parallel task for each email address found. This example is presented in Example: Accelerating the processes. - improve the reactivity of the application: several long and locking processes are run in parallel instead of being run sequentially: the user does not feel that he is stuck.
A simple example: A Table control displays a list of contacts whose photo is loaded from an Internet address. For each contact, the application preforms an Internet request (that triggers a slow down). In order for the Table control to be filled without being locked, the process for row display starts a parallel task used to start the Internet request and to refresh the GUI if necessary. This example is presented in Example: Improving the reactivity of application.
To manage the parallel tasks, WLanguage proposes: - a variable of type ParallelTask. This type of variable is used to handle a parallel task. This type of variable cannot be used to modify the characteristics of a parallel task.
- functions for managing tasks (ParallelTask*).
Remark: You also have the ability to use the Description of ParallelTask variable. This type of variable can only be used to describe a parallel task. Once the parallel task is defined, its characteristics cannot be modified. Principle To implement a management of parallel tasks, you must: - Create and run a parallel task.
- Wait for the execution of the task and retrieve the return value.
- Manage (if necessary) the sequence of parallel tasks.
- Handle (if necessary) the controls from a parallel task.
Create and run a parallel task A parallel task must be associated with a ParallelTask variable. Several methods can be used to declare a ParallelTask variable: - Simple declaration. The parallel task is described during its execution with ParallelTaskExecute:
// Declares a variable to handle a parallel task t is ParallelTask // Run and describe the parallel task t = ParallelTaskExecute(Proc, ("First parameter", 2))
- Declaration and description of the parallel task. Then, the parallel task is run by ParallelTaskExecute.
t is ParallelTask(Proc, ("First parameter", 2))
ParallelTaskExecute(t)
Remark: When describing the parallel task, you have the ability to specify: - the procedure to run.
- the parameters expected by the procedure.
- the runtime mode of parallel task: management of HFSQL contexts and interactions with the main thread.
MyParallelTask keyword MyParallelTask is used to manage the current parallel task and find out its properties. This allows you to access information about the current parallel task in the code executed by a parallel task. The accessible properties are those of ParallelTask variables:
| | | Property name | Type used | Effect |
---|
Canceled | Boolean | - True if the task is canceled,
- False otherwise.
This property is read-only. | Completed | Boolean | - True if the task is completed,
- False otherwise.
This property is read-only. | New in version 2025Extra | Variant | Allows you to store advanced information without affecting the execution of the application. You can store values of any type (array, etc.). It is also possible to add members to the Extra property. Example:
MyVariable.Extra.Info1 = Value MyVariable.Extra[Info2] = Value2 MyVariable.Extra.Date = DateSys() | Identifier | Integer | Task identifier. This identifier can be used for debugging purpose for example. This property is read-only. | ReturnedValue | Value returned by the task. | Caution: - If the task is still in progress, the ReturnedValue property waits for the end of the task
- If the task is completed without fatal error, the property returns the return values of the procedure of the task.
This property is read-only. | Status | Integer constant | Status of the task: - tpeCancelled: parallel job is cancelled (function ParallelTaskCancel).
- tpeRequestedCancellation: a cancellation request has been made on the parallel task (function ParallelTaskRequestCancellation)..
- tpeAttenteExécution: parallel task is waiting to be executed.
- tpePreviousPreviousPrevious: the parallel task waits for a previous parallel task to run.
- tpeExecutionInProgress: parallel task is currently running.
- tpeNotScheduled: parallel task is not scheduled.
- tpeTerminated: parallel task completed.
This property is read-only. |
Wait for the execution of the task and retrieve the return value Several parallel tasks can be started at the same time. You have the ability to wait for the execution of one or more parallel tasks before running a process: The ValeurRenvoyée property of the ParallelTask variable gets the value returned by the procedure executed by the parallel task. Caution: This value is available only if the parallel task is ended. If the task is in progress, the call to this property is locking until the end of the task. Managing the sequence of parallel tasks Several parallel tasks can be started at the same time. You have the ability to define the sequence of parallel tasks: a task can wait for the end of execution of one or more tasks before it is run. The following functions are used to define a continuation task: | | ParallelTaskExecuteAfter | Indicates a continuation parallel task that will be run when one of the specified parallel task is completed. | ParallelTaskExecuteAfterAll | Indicates a continuation parallel task that will be run when all the tasks found in an array of parallel tasks are ended. | ParallelTaskExecuteAfterOne | Indicates a continuation parallel task that will be run once the first task is completed in an array of parallel tasks. |
Remark: In a continuation task, you can: Handling the controls from a parallel task You cannot act on the interface from a parallel task. Therefore, you cannot assign a control, fill a Table or Looper control. Only a task defined by the ptoMainThread constant can be run in the main thread and can update the controls if necessary. Remark: You can also use ExecuteMainThread to execute a specific display procedure from the parallel task. Example: Accelerating the processes Accelerating the processes: statistical calculation An application uses the CalculateStatEmailAddress procedure to perform statistical calculations about the send and receive operations on each email address found in CUSTOMER file. This procedure takes the email address as parameter and it calculates all the statistics for this address. If the statistical calculation on an email address takes one second and if the database contains 200 000 email addresses, the calculation takes more than two days (200 000 seconds). To go faster, you have the ability to start a parallel task for each email address found. Code example: - Initial code (before using parallel tasks):
nAddressesInError is int  HourGlass(True) ChronoStart() // Browses the list of customers FOR EACH Customer // Starts the statistical calculation on the email address IF CalculateStatEmailAddress(Customer.Email, 1) = False THEN nAddressesInError++ END END HourGlass(False) STC_Result_1 = StringBuild("Result: %1 addresses in error", nAddressesInError) Info("Process completed", DurationToString(ChronoEnd(), "MMm SSs CCC"))
- Code thai is using the parallel tasks:
nAddressesInError is int arrTasks is array of ParallelTasks ATask is ParallelTask  HourGlass(True) ChronoStart() // Browses the list of customers FOR EACH Customer // Starts the statistical calculation on the email address via a parallel task ATask = ParallelTaskExecute(CalculateStatEmailAddress, ... (Customer.Email, 1), ptoLightCopyHFSQLContext) // Stores this task in an array Add(arrTasks, ATask) END  // Waits for the end of task execution ParallelTaskWaitAll(arrTasks) HourGlass(False)  // Browses the tasks FOR EACH ATask OF arrTasks IF ATask..ReturnedValue = False THEN nAddressesInError++ END END STC_Result_2 = StringBuild("Result: %1 addresses in error", nAddressesInError) Info("Process completed", DurationToString(ChronoEnd(), "MMm SSs CCC"))
Example: Improving the reactivity of application Improving the reactivity of application A Table control displays a list of contacts whose photo is loaded from an Internet address. For each contact, the application preforms an Internet request (that triggers a slow down). To improve the reactivity of application and to get a smooth GUI, a parallel task is started in the process for displaying a row of Table control. This parallel task: - receives the contact identifier in prameter.
- performs the Internet request to get the image.
- retrieves the image.
- calls a function to update the Table control.
Let's see two different codes for this example: these two codes present two different methods for updating the GUI: Updating the GUI via a continuation task The interface update not being possible from a parallel task run in the main thread, a specific continuation task for the display is implemented by ParallelTaskExecuteAfter. One of the parameters passed to the continuation task corresponds to the value returned by the main parallel task. To specify this parameter, all you have to do is use the ReturnedValuePreviousTask keyword. Code example: - Code for displaying a table row:
// If the photo is not filled yet IF COL_Photo ~= "" THEN // Positions the hourglass image while waiting for the photo to be retrieved from "Internet" COL_Photo = IMG_Hourglass // Starts retrieving the photo in a parallel task MyTaskFindImage is ParallelTask = ParallelTaskExecute(FindImage, ... (COL_CustomerNum), ptoLightCopyHFSQLContext) // Starts displaying the image in a continuation task // that interacts with the interface ParallelTaskExecuteAfter(MyTaskFindImage, DisplayImage, ... (COL_CustomerNum, ReturnedValuePreviousTask), ptoMainThread) END
- Code of "FindImage" procedure: This process is used to retrieve the image.
PROCÉDURE FindImage(LOCAL nCustomerID is 8-byte int)  // Retrieve the photo Result1 is boolean = HTTPRequest("http://Linkedin.com/photos/id=" + ID) IF Result1 = True THEN bufPhoto is Buffer = HTTPGetResult() bufPhoto = fLoadBuffer(fExeDir() + fSep() + "Photos\" + ID + ".jpg") END  RESULT bufPhoto
- Code of "DisplayImage" procedure: This procedure is used to display the image in the table.
PROCÉDURE DisplayImage(nCustomerID is 8-byte int, sPhotoPath is string) // Finds the customer in the table nIndex is int = TableSearch("WIN_MENU.TABLE_Customer.COL_CustomerNum", nCustomerID) IF nIndex > 0 THEN // Displays the customer photo WIN_Menu.TABLE_Customer.COL_Photo[nIndex] = sPhotoPath END
Updating the GUI via a procedure run in the main thread The controls of the window cannot be accessed from a parallel task. To display the image, the DisplayImage procedure is run by ExecuteMainThread. This function forces the procedure execution in the main thread. You also have the ability to indicate to the DisplayImage procedure that it will always be run in the main thread. All you have to do is click the button in the bar of the code editor and check "Run in the main thread". Code example: - Code for displaying a table row:
// If the photo is not filled yet IF COL_Photo ~= "" THEN // Positions the hourglass image while waiting for the photo to be retrieved from "Internet" COL_Photo  = IMG_Hourglass // Starts retrieving the photo in a parallel task MyTaskFindImage is ParallelTask = ParallelTaskExecute(FindImage, ... (COL_CustomerNum), ptoLightCopyHFSQLContext) END
- Code of "FindImage" procedure: This process is used to retrieve the image.
PROCÉDURE FindImage(LOCAL nCustomerID is 8-byte int)  // Retrieve the photo Result1 is boolean = HTTPRequest("http://Linkedin.com/photos/id=" + ID) IF Result1 = True THEN bufPhoto is Buffer = HTTPGetResult() bufPhoto = fLoadBuffer(fExeDir() + fSep() + "Photos\"+ ID + ".jpg") END  // Calls the procedure to display the image ExecuteMainThread(DisplayImage, nCustomerID, bufPhoto)
- Code of "DisplayImage" procedure: This procedure is used to display the image in the table.
PROCÉDURE DisplayImage(nCustomerID is 8-byte int, sPhotoPath is string) // Finds the customer in the table nIndex is int = TableSearch("WIN_MENU.TABLE_Customer.COL_CustomerNum", nCustomerID) IF nIndex > 0 THEN // Displays the customer photo WIN_Menu.TABLE_Customer.COL_Photo[nIndex] = sPhotoPath END
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|