Reputation: 313
I have an issue concerning a Thread lacking a message queue at the begin of its life cycle. MSDN explains
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation:
(1) Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
(2) Create an event object, then create the thread. Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage. In the thread to which the message will be posted, call
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
to force the system to create the message queue. Set the event, to indicate that the thread is ready to receive posted messages.
Method (1) solves my issue, the second call to PostThreadMethod()
always succeeds in my application.
However, I would like to comprehend the second method and simply don't understand "event object" (certainly not a normal Delphi event?) "to the signalled state" and "set the event to indicate".
QUESTION: Can someone please be so kind as to translate paragraph (2) into a short Delphi code example?
Upvotes: 3
Views: 1222
Reputation: 612884
These event objects are synchronization objects, described in MSDN here: Event Objects.
At the bottom of that topic is a link to Using Event Objects which gives example code showing how to create events, set them, wait for them, etc.
In short you use the following functions:
CreateEvent
to create the event objects.CloseHandle
to destroy it.SetEvent
and ResetEvent
to set and reset the event object.WaitForSingleObject
to wait for it to be signaled.You can use the TEvent
class from the System.SyncObjs
unit to wrap all of these low-level API calls. Then the process would become like so:
TEvent
object, Event
say, in the reset state.Event
.Event.WaitFor
in the manager thread to wait for the worker thread to signal that its message queue exists.Execute
method), have it create its message queue, and then set the event by calling Event.SetEvent
.Upvotes: 5