- Opening a window from a secondary thread via a dialog by messages
- The above-mentioned method is used in the example named WD Instant messaging.
Opening a window from a secondary thread
A secondary thread cannot directly open a window with the standard WLanguage functions such as: Open
Nevertheless, you may want to display a window from a secondary thread. For example, if a thread is used to manage some incoming calls, a window containing the characteristics of the caller can be displayed by this thread when an incoming call is detected.
The solution consists in opening the window from a code run by the main thread.
The following methods can be used to execute the code of the main thread from a secondary thread:
- Run a global procedure of the project or a local procedure of a window via ExecuteMainThread,
- Run the event "Request for refreshing the display" of a window via RequestUpdateUI,
- Establish a dialog via messages between threads (PostMessage and Event).
Opening a window from a secondary thread via a dialog by messages
To open a window from a secondary thread:
- When the secondary thread wants to open a window, it sends a message to the main thread with PostMessage. This message indicates to the main thread the window that must be opened as well as the parameters required to open the window.
- The main thread manages a specific event, corresponding to the request for window opening. When this event occurs, the WLanguage procedure opens the specified window (with Open for example) with the specified parameters.
The above-mentioned method is used in the example named WD Instant messaging.
The code for opening the main window is used to:
- implement an event to manage the opening of the window in the main thread.
- trigger the secondary threads.
// Implement an event
// to manage the opening of the "chat" window
gnEventID = Event("ChatOpening", "*.*", "ChatStart")
IF gnEventID = 0 THEN
// Error when implementing
// the event used to manage the "chat" window
Error("Unable to manage the window for chat opening", ErrorInfo())
// The application cannot operate without this event
// Listen to the requests for connection and for presence tests
ThreadExecute("ThreadPresence", threadNormal, "ListenPresenceRequest")
ThreadExecute("ThreadRequest", threadNormal, "ListenConnectionRequest")
The window is opened in the code of the ListenRequestStartMessage procedure for example. This procedure is run in a thread. The code used to open the window from the thread is as follows:
// Ask to open the "chat" window
PostMessage(Handle(WIN_Main),"ChatStart", FILE_CONTACT.CONTACTID, FILE_CONTACT.CONTACTID)
The "ChatStart" message is sent to the main window (identified by its handle). This message contains the different parameters that must be passed to the window. The message is handled by the event defined in the opening code of the window. When the message occurs, the "ChatOpening" procedure is automatically run. This procedure is used to open the window.
The code of this procedure is as follows:
This page is also available for…