|
- Overview
- Simple management of threads
- Principle
- WLanguage functions
- Characteristics of a thread
- Characteristics of 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
Principle for running threads
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 New in version 17 Versions 21 and later New in version 21 Simple management of threads Principle 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 laterof the current thread, use ThreadEnd. New in version 19of 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:
| | ThreadCurrent | Returns the name of the thread currently run. | ThreadEnd | Ends the execution of the current thread. | ThreadExecute | Starts the execution of a secondary thread. | ThreadMode | Changes the management mode of threads. | ThreadPause | Pauses the current thread during the specified duration. | ThreadPriority | Returns or modifies the priority level of a thread. | ThreadResume | Resumes the execution of a thread that was interrupted by ThreadSuspend. Function not recommended. | ThreadState | Returns the current status of a thread. | ThreadStop | Stops a secondary thread. Function not recommended. | ThreadSuspend | Temporarily suspends the execution of the specified thread. Function not recommended. | ThreadWait | Waits for the end of execution of specified thread. |
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.
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). See Assisted management of HFSQL errors for more details.
- Write operations and assignments in a thread: If write operations or assignments are performed in a thread, the other threads currently run do not share this information. Some inconsistencies may occur.
Example: | | Code of Thread 1 | Code 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. Recommendations for the processes performed by the thread Forbidden processes Caution: The following processes cannot be run in the threads: | Caution: it is not allowed to handle the interface (windows, controls, etc.) in a secondary thread. When a secondary thread must interact with the user or update the interface, 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: - Change the default management mode of threads (ThreadMode).
- 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:
|
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
|
|
Unit examples (WINDEV Mobile): The threads (pool)
[ + ] Using threads with the WLanguage functions. Two main topics are presented in this example: 1/ Run processes started in parallel (threads) 2/ Limit the number of threads in execution at a given time Reminder: a "thread" is a process started in parallel of 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
|
|
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
|
|
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 …
|
|
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.
|
This page is also available for…
|
|
|
| |
| Click [Add] to post a comment |
|
| |
|
| |
| |
| |
| |
| |
| |
| | |
| |