PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • Principle
  • How to proceed?
  • Principle
  • Create and run a parallel task
  • 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
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
The computers are becoming more and more powerful. The computers have powerful processors with several cores.
To improve the performances 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 the 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.
Versions 23 and later
Java This feature is now available for the Java applications.
Android Widget This feature is now available in Android Widget mode.
Android This feature is now available for the Android applications.
New in version 23
Java This feature is now available for the Java applications.
Android Widget This feature is now available in Android Widget mode.
Android This feature is now available for the Android applications.
Java This feature is now available for the Java applications.
Android Widget This feature is now available in Android Widget mode.
Android This feature is now available for the Android applications.
Principle
To manage the parallel tasks, WLanguage proposes:
  • a ParallelTask variable. 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*).
Note: 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.
How to proceed?

Principle

To implement a management of parallel tasks, you must:
  1. Create and run a parallel task.
  2. Wait for the execution of the task and retrieve the return value.
  3. Manage (if necessary) the sequence of parallel tasks.
  4. 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.
    // Builds a parallel task
    t is ParallelTask(Proc, ("First parameter", 2))
    // Triggers the execution of parallel task
    ParallelTaskExecute(t)
Note: 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.

Wait for the execution of the task and retrieve the return value

Sevaral 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:
Versions 20 and later
ParallelTaskWait
New in version 20
ParallelTaskWait
ParallelTaskWait
Waits for the execution of a parallel task.
Versions 20 and later
ParallelTaskWaitAll
New in version 20
ParallelTaskWaitAll
ParallelTaskWaitAll
Waits for the end of execution of all parallel tasks found in an array.
The ..ReturnedValue property of the ParallelTask variable is used to find out the value returned by the procedure run 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

Sevaral 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:
Versions 20 and later
ParallelTaskExecuteAfter
New in version 20
ParallelTaskExecuteAfter
ParallelTaskExecuteAfter
Indicates a continuation parallel task that will be run when one of the specified parallel task is ended.
Versions 20 and later
ParallelTaskExecuteAfterAll
ParallelTaskExecuteAfterAll
Indicates a continuation parallel task that will be run when all the tasks found in an array of parallel tasks are ended.
Versions 20 and later
ParallelTaskExecuteAfterOne
ParallelTaskExecuteAfterOne
Indicates a continuation parallel task that will be run once the first task is ended in an array of parallel tasks.
Note: 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.
Note: You also have the ability to use ExecuteMainThread to run a specific display procedure from the parallel task.
These two methods are used in the Improving the reactivity of application example presented in this page.
Example: Accelerating the processes

Accelerating the processes: statistical calculation

An application is using 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 in 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 sample:
  • 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 LLL"))
  • 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 LLL"))
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 sample:
  • 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
    nSubscript is int = TableSeek("WIN_MENU.TABLE_Customer.COL_CustomerNum", nCustomerID)
    IF nSubscript > 0 THEN
    // Displays the customer photo
    WIN_Menu.TABLE_Customer.COL_Photo[nSubscript] = 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 execution of a procedure 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 sample:
  • 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.
    PROCEDURE 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
    nSubscript is int = TableSeek("WIN_MENU.TABLE_Customer.COL_CustomerNum", nCustomerID)
    IF nSubscript > 0 THEN
    // Displays the customer photo
    WIN_Menu.TABLE_Customer.COL_Photo[nSubscript] = sPhotoPath
    END
Minimum required version
  • Version 20
This page is also available for…
Comments
Click [Add] to post a comment