PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
This content has been translated automatically. Click here to view the French version.
  • 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
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
At runtime, an application runs in a main thread.
This application can start a secondary thread at any time: this thread is run 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. All processes that can be run in the background can be performed in this thread: receiving emails, etc.
Remark: an efficient thread is a thread that waits for a specific event such as a user action, an incoming phone call or email, ...
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:
  • of a secondary Thread:
    1. use the ThreadDemandStop function,
    2. test the result of the threadStopRequested function in the secondary the thread in order to end the execution of the thread with an exit of the Procedure, or a call of the ThreadEnd function.
Caution: If a WLanguage function is currently run when a thread is stopped, the thread will actually stop after the execution of the function.

WLanguage functions

The following functions are used to manage threads:
ThreadStopRequestedChecks if a stop request has been sent to the running thread.
ThreadStopStops a secondary thread. Function not recommended.
ThreadWaitWaits for the end of the execution of the specified thread.
ThreadCurrentReturns the name of the thread currently run.
ThreadRequestStopSends a stop request to a thread.
ThreadStateReturns the current status of a thread.
ThreadExecuteStarts the execution of a secondary thread.
ThreadEndEnds the execution of the current thread.
ThreadModeChanges the management mode of threads.
ThreadPausePauses the current thread during the specified duration.
ThreadPriorityReturns or modifies the priority level of a thread.
ThreadResumeResumes the execution of a thread that was interrupted by ThreadSuspend. Function not recommended.
ThreadSuspendTemporarily suspends the execution of the specified thread. Function not recommended.

For more details, see Thread management 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 thrown:
    • 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).

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>.
New in version 28
<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>.
<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>.
Universal Windows 10 App This extension attribute is not available.
<full HFSQL context>Triggers the immediate copy of the current HFSQL context.
Recommended if the thread must take into account the current positions in the files and queries of caller context.

This extension attribute must be used with <thread>.
Universal Windows 10 App This extension attribute is not available.

Remark: You can also 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 in which the secondary thread was created (the main thread in most cases).
Therefore, these threads can access the variables, procedures, ... All the variables created once a thread is started are accessible in the thread where they have been 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 management of HFSQL errors: If several threads are used on the data files, we advise you to customize the management of HFSQL errors to avoid displaying the default windows. 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 Assisted HFSQL error handling.
  • Write operations and assignments in a thread: If values are written or assigned in a thread, the 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.
Remark: To share an assignment between several threads, you must use semaphores. For more details, see Managing the semaphores in the threads.
Recommendations for the processes performed by the thread

Forbidden processes

Caution: The following processes cannot be run in the threads:
  • opening windows with WLanguage functions such as Open up, Use, ferme, ... 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.
Caution: UI elements (windows, controls, etc.) cannot be manipulated 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.
Java In Java, events (click code of a button, for example), procedures and class methods can be run by several threads at the same time. To prevent several threads from running the same code at the same time, critical sections or semaphores must be added to 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.
Related Examples:
The threads Unit examples (WINDEV Mobile): The threads
[ + ] Using threads in an application:
- run a procedure in thread format
- stop a thread
- run a procedure that expects parameters in thread format
The threads Unit examples (WINDEV): The threads
[ + ] Using threads in an application:
- Run a procedure in thread format
- Stop a thread
- Run a procedure that expects parameters in thread format
The threads (pool) Unit examples (WINDEV): The threads (pool)
[ + ] Using threads:
- Running processes started in parallel
- Limiting the number of threads in execution at a given time
Reminder: A thread is a process running in parallel to the current application (main thread).
This allows you to run a task in background task for example (backup, ...).
Threads are very useful in the industry: supervisor, applications with real-time process check, and so on …
WD Using sockets Training (WINDEV): WD Using sockets
[ + ] This example presents the use of the "Socket" functions of WINDEV in Client/Server.
The following topics are presented in this example:
1/ How to connect to a socket
2/ How to accept a request for connection
3/ How to write or read on a socket
Summary of the example supplied with WINDEV:
This example presents the different functions for socket management supplied with WINDEV. To use this example, a first instance of the example must be started in "server" mode. Then, by specifying the name of the computer that will be used as server and a nickname, you can connect to this application in "client" mode. You now have the ability to send messages to all the connected users. In this example, a thread and a socket are associated with each connection.
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment