PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
  • 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, ...
Remark: From version 19, HFSQL is the new name of HyperFileSQL.
Versions 17 and later
Universal Windows 10 App This feature is now available for Windows Phone applications.
New in version 17
Universal Windows 10 App This feature is now available for Windows Phone applications.
Universal Windows 10 App This feature is now available for Windows Phone applications.
Versions 21 and later
Universal Windows 10 App This feature is now available in Universal Windows 10 App mode.
New in version 21
Universal Windows 10 App This feature is now available in Universal Windows 10 App mode.
Universal Windows 10 App This feature is now available in Universal Windows 10 App mode.
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, use ThreadStop.
  • Versions 19 and later
    of the current thread, use ThreadEnd.
    New in version 19
    of the current thread, use ThreadEnd.
    of the current thread, use ThreadEnd.
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 the threads:
Versions 16 and later
ThreadCurrent
New in version 16
ThreadCurrent
ThreadCurrent
Returns the name of the thread currently run.
Versions 19 and later
ThreadEnd
New in version 19
ThreadEnd
ThreadEnd
Ends the execution of the current thread.
Versions 09 and later
ThreadExecute
New in version 09
ThreadExecute
ThreadExecute
Starts the execution of a secondary thread.
Versions 09 and later
ThreadMode
New in version 09
ThreadMode
ThreadMode
Changes the management mode of threads.
Versions 10 and later
ThreadPause
New in version 10
ThreadPause
ThreadPause
Pauses the current thread during the specified duration.
Versions 09 and later
ThreadPriority
New in version 09
ThreadPriority
ThreadPriority
Returns or modifies the priority level of a thread.
Versions 09 and later
ThreadResume
New in version 09
ThreadResume
ThreadResume
Resumes the execution of a thread that was interrupted by ThreadSuspend. Function not recommended.
Versions 09 and later
ThreadState
New in version 09
ThreadState
ThreadState
Returns the current status of a thread.
Versions 09 and later
ThreadStop
New in version 09
ThreadStop
ThreadStop
Stops a secondary thread. Function not recommended.
Versions 09 and later
ThreadSuspend
New in version 09
ThreadSuspend
ThreadSuspend
Temporarily suspends the execution of the specified thread. Function not recommended.
Versions 09 and later
ThreadWait
New in version 09
ThreadWait
ThreadWait
Waits for the end of execution of specified thread.

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.
Versions 26 and later
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 ThreadStop 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).
New in version 26
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 ThreadStop 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).
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 ThreadStop 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).
Versions 24 and later

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 run in a secondary thread.
This extension attribute is not compatible with <timer> and <thread>.
Versions 26 and later
<secure thread>
New in version 26
<secure 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.
New in version 24

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 run in a secondary thread.
This extension attribute is not compatible with <timer> and <thread>.
Versions 26 and later
<secure thread>
New in version 26
<secure 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.

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 run in a secondary thread.
This extension attribute is not compatible with <timer> and <thread>.
Versions 26 and later
<secure thread>
New in version 26
<secure 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 browse operations on the same data file in two different threads.
Example:
  • A filter is created on the Customer data file.
  • ThreadExecute is used to create the thread named CTX2.
  • 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 management of HFSQL errors.
  • 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, Use, Close, ... 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 event "Request for refreshing the display" 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.
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