AYUDA EN LÍNEA
 WINDEVWEBDEV Y WINDEV MOBILE

Este contenido se ha traducido automáticamente.  Haga clic aquí  para ver la versión en inglés.
Ayuda / WLanguage / Funciones WLanguage / Funciones estándar / Funciones de gestión de procesos / Hilos, semáforos, señales y mutex
  • Overview
  • Simple management of threads
  • Principle
  • WLanguage functions
  • Characteristics of a thread
  • Characteristics of threads
  • Extension attributes related to threads
  • Access to existing elements and HFSQL context
  • Recommendations for the processes performed by the thread
  • Forbidden processes
  • Processes of a WINDEV application
  • Exception process and threads
WINDEV
WindowsLinuxJavaReportes y ConsultasCódigo de Usuario (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Código Navegador
WINDEV Mobile
AndroidWidget Android iPhone/iPadIOS WidgetApple WatchMac Catalyst
Otros
Procedimientos almacenados
Overview
At runtime, an application runs in a main thread.
At any time, this application can launch a secondary thread This thread runs in parallel with the application. It corresponds to a local or global procedure of the application.
The secondary thread is run in parallel with the main application. This thread can be used for all background processing, such as e-mail reception.
Note An efficient thread is a thread that waits for a specific event, e.g. a user action, the receipt of a phone call or e-mail, etc., to occur.
Simple management of threads

Principle

A secondary thread is created by ThreadExecute.
A secondary thread is automatically stopped when:
  • the procedure corresponding to the thread is ended,
  • the object that created the thread is closed.
To force the stop:
Please note If a WLanguage function is running when a thread is stopped, the thread will not be stopped until the function has been executed.

WLanguage functions

The following functions are used to manage threads:
ThreadCurrentDevuelve el nombre del hilo que se está ejecutando actualmente.
ThreadEndFinaliza la ejecución del subproceso actual.
ThreadExecuteStarts the execution of a secondary thread.
ThreadModeChanges the management mode of threads.
ThreadPausePauses the current thread during the specified duration.
ThreadPriorityDevuelve o modifica el nivel de prioridad de un hilo.
ThreadRequestStopEnvía una solicitud para detener un subproceso.
ThreadResumeResumes the execution of a thread that was interrupted by ThreadSuspend. Function not recommended.
ThreadStateDevuelve el estado actual de un hilo.
ThreadStopDetiene un subproceso secundario. Función no recomendada.
ThreadStopRequestedVerifica si se ha enviado una solicitud al hilo que se está ejecutando.
ThreadSuspendTemporarily suspends the execution of the specified thread. Function not recommended.
ThreadWaitEspera a que finalice la ejecución del subproceso especificado.

For more details, see Thread functions on all the functions used to manage threads.
Characteristics of a thread

Characteristics of threads

In WLanguage, a secondary thread can be associated with:
  • a procedure local to the current window,
  • a procedure global to the project,
  • a method of a class,
  • a global method of a class.
A secondary thread can be a secure thread. In this mode:
  • a compilation error will occur if the controls are accessed in the thread (or if the procedure uses the "UI" attribute).
  • an exception will be raised:
    • if the thread accesses the controls at runtime,
    • if the ThreadStop function is called.
  • when the window that triggered the thread is closed, a request to stop the thread is generated (but the thread continues to run after the window is closed).
Novedad versión 2025
Android Secure threads are now available.

Extension attributes related to threads

When declaring a procedure or a class method, you can use extension attributes to specify the characteristics of the thread:
<thread>Used to specify that the procedure will be run in a secondary thread.
This extension attribute is not compatible with <timer> and <main thread>.
<main thread>Used to specify that the procedure will be executed in the main thread.
This extension attribute is not compatible with <timer> and <thread>.
<main thread asynchronous>Indicates that the procedure will be executed in the main thread and that there is no need to wait for the end of the execution of this procedure.
This extension attribute is not compatible with <timer> and <thread>.
<secure thread>Used to specify that the procedure will be run in a secure secondary thread.
This extension attribute is not compatible with <timer>, <main thread> and <UI>.
Novedad versión 2025
Android Secure threads are now available.
<light HFSQL context>Triggers the immediate copy of a part of the current HFSQL context.
Only the directories containing the data files in HFSQL Classic mode and/or the connections in HFSQL Client/Server mode are stored.

This extension attribute must be used with <thread>.
<full HFSQL context>Triggers the immediate copy of the current HFSQL context.
Recommended if the thread needs to take into account the current positions in the files and queries of the caller's context.

This extension attribute must be used with <thread>.

Note: It's also possible to use the code editor interface, via Automatic procedures.

Access to existing elements and HFSQL context

When creating a thread, all the existing declarations, objects, elements are common:
  • to the new secondary thread.
  • to the thread where the secondary thread was created (the main thread in most cases).
These threads can access variables, procedures, etc. All variables created after a thread has been started can only be accessed in the thread in which they were created.
Similarly, when creating a thread, the HFSQL context is automatically duplicated. Each thread handles a specific HFSQL context. The number of HFSQL contexts is equal to the number of threads currently run. The entire HFSQL context is copied (filter, search condition, etc.). The HFSQL context evolves independently in each thread.
This allows you, for example, to perform two different iterations on the same data file in two different threads.
Example:
  • A filter is created on the Customer data file.
  • ThreadExecute is called to create the CTX2 thread.
  • The Customer data file is filtered in each thread (main and CTX2). The filter will still be enabled in the CTX2 thread even if the filter is disabled in the main thread.
Special cases:
  • Assisted HFSQL error handling If several threads are used on data files, it is advisable to customize HFSQL error handling so that the default windows are not displayed. To do so, use HOnError to disable the automatic management of errors or to redirect the management of errors to a custom procedure (without displaying windows). For more details, see HFSQL error handling help.
  • Writes and assigns in a thread If writes or assigns are performed in a thread, other running threads do not share this information. Some inconsistencies may occur.
Example:
Code of Thread 1Code of Thread 2
a=i
a++
i=a
b=i
b++
i=b

These two threads share the variables but they do not manage the access to the common resources. If the thread 1 is run before the thread 2, i will be set to 1 instead of 2.
Note To share an assignment between several threads, it is necessary to use semaphores. For more details, see Managing the semaphores in the threads.
Recommendations for the processes performed by the thread

Forbidden processes

Please note: the following processes cannot be run in threads:
  • opening windows with WLanguage functions such as Open, Use, Close, etc. A specific management mode must be implemented if some windows must be handled in threads (rare case). For more details, see Opening a window in a secondary thread.
  • managing events.
  • multitask.
  • managing timers.
Warning: it is forbidden to manipulate the UI (windows, fields, etc.) in a secondary thread.
When a secondary thread must interact with the user or update the UI, it must use a process started from the main thread. This process can correspond to:
  • a global procedure of the project or a local procedure (of a window, etc.) called by ExecuteMainThread,
  • the "Request for refreshing the display" event of a window run by RequestRefreshUI.

Processes of a WINDEV application

By default, WINDEV events (click code of a button, for example), procedures and class methods can only be run by a single thread at a given time.
To allow several threads to run these processes at the same time, you must:
  1. Change the default management mode of threads (ThreadMode).
  2. Manage critical sections and semaphores in the code of the application.

Exception process and threads

If a general exception process is set in the project initialization code, it will be triggered if an exception occurs:
  • in the main thread,
  • in a secondary thread started by ThreadExecute.
However, if the secondary thread triggers an exception, it will not be possible to find out its origin with ExceptionInfo in the project code. To find out the origin of an exception in a secondary thread, the exception process must be included in the secondary thread.
Versión mínima requerida
  • Versión 9
Esta página también está disponible para…
Comentarios
Haga clic en [Agregar] para publicar un comentario

Última modificación: 27/03/2025

Señalar un error o enviar una sugerencia | Ayuda local