ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Process functions / Threads, semaphores, signals and mutex
  • Overview
  • Simple management of events
  • Principle
  • Example: Application managing a spelling checker
  • WLanguage functions
  • Remark
  • Advanced management of events
  • Principle
  • Implementation
  • Functions for advanced management of events
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
Events can be used to synchronize the different threads of an application. Therefore, a thread can wait for the execution of another thread.
Two management modes can be used to manage events:
Simple management of events

Principle

Two threads are run in parallel (a main thread and a secondary thread for instance). One of the threads waits for a specific action from the second thread before it can be run.

Example: Application managing a spelling checker

When the user types the SPACE character in an edit control, the spelling checker is automatically started to check the previous word.
In this case, the spell check is managed in a secondary thread.
Whenever the SPACE key is pressed, the main thread sends a signal to the secondary thread in order to start the spelling checker.
The code for this kind of application is as follows:
  • Code of the main thread:
    IF Right(EDT_Edit1, 1) = " " THEN
    ThreadSendSignal("ThreadSpellCheck")
    END
  • Code of the secondary thread (ThreadSpellCheck):
    LOOP
    IF ThreadWaitSignal() = True THEN
     StartSpellCheck()
    END
    END

WLanguage functions

The following functions are used to perform a simple management of events:
ThreadSendSignalThe current thread sends a signal to the specified thread in order to unlock it.
ThreadWaitSignalLocks the current thread until it receives a signal from another thread.

Remark

Semaphores and events are system objects identified by their name. They are shared among all the applications that run on a computer.
Therefore, two applications (or two instances of the same application) use the same objects if the same name is used.
To create unique names, use Instance to implement the desired name.
Java In Java, semaphores, critical sections and events are unique for the application that created them. They cannot be shared between several applications.
Advanced management of events

Principle

An advanced management of events consists in communicating between several threads (more than 2). Some threads are waiting for a task performed by the main thread. When the main thread performs this task, it sends a signal to all the secondary threads.

Implementation

Two methods can be used to implement an advanced management of events:
  • Using AutomaticEvent and ManualEvent.
  • Using named events (recommended method to manage the options to share events).
WINDEVUniversal Windows 10 App Method 1: Using EventAutomatic and EventManual.
The steps for implementing an advanced management of events are as follows:
  1. Creating a variable of type AutomaticEvent or ManualEvent. By default, this event is closed. The event is automatically destroyed when going outside the scope of the corresponding variable.
  2. Waiting an event (EventWait).
  3. Synchronizing the threads with EventOpen and EventClose.
Method 2: Using named events (recommended method to manage the options to share events)
The steps for implementing an advanced management of events are as follows:
  1. Creating an event (EventCreate). By default, this event is closed.
  2. Waiting an event (EventWait).
  3. Synchronizing threads:
    • with the EventChange function: the event is opened. All waiting threads are unlocked and the event is automatically closed (default operating mode).
    • WINDEVUniversal Windows 10 App via EventOpen and EventClose.
  4. Destroying the event (EventDestroy).

Functions for advanced management of events

The following functions are used for an advanced management of events:
EventChangeModifies the status of an event.
EventCloseCloses a synchronization event between several threads.
EventCreateCreates an event.
EventDestroyExplicitly destroys an event.
EventOpenOpens a synchronization event between several threads.
EventWaitLocks the current thread while waiting for the specified event to be opened.
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 07/04/2023

Send a report | Local help