3

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?

5
  • Note that I removed the second question because at this site it is one question at a time. What's more, the second question would fall foul of the "primarily opinion based" close reason. Commented Apr 2, 2018 at 11:57
  • @David: OK, maybe you can shortly comment on whether there are in your opinion better inter-thread communication methods than PostThreadMessage()? I want worker threads to say "started" and "finished" to a managing thread which adds and remove worker threads from a list. Commented Apr 2, 2018 at 12:27
  • You certainly don't want to use thread messages. Because they will get lost if you show any modal UI. Using a message only window is the way to do it with messages. As for whether that's the right solution to your problem, I can't say with so little background. Commented Apr 2, 2018 at 12:45
  • 1
    Hm... all threads involved, i.e. worker threads and managing threads have no UI at all. I thought, threads may not use VCL anyway? Only the main thread has a UI. Is this a problem? It appears to work with TEvent and it did work with repeated first PostMessage, too. Commented Apr 2, 2018 at 12:52
  • Often the managing thread is also the UI thread. If you have no UI in any of these threads then perhaps thread message is fine. Commented Apr 2, 2018 at 12:59

1 Answer 1

5

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:

  • Create a TEvent object, Event say, in the reset state.
  • Create your worker thread, passing in Event.
  • Call Event.WaitFor in the manager thread to wait for the worker thread to signal that its message queue exists.
  • When the worker thread starts executing (i.e. at the start of its Execute method), have it create its message queue, and then set the event by calling Event.SetEvent.
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you. SyncObjs.TEvent it is. -- Are TEVents restricted to the current process? If several instances of the same application are running, will they see the events of one another or not? Do I have to use instance-unique TEvent names in order to avoid this?
You can have named events but I don't think the RTL classes wrap that functionality
TEvent supports "name" and it works for me now. I just don't know whether this name will work inter-instance or is restricted to the current instance.
Object naming is covered over on MSDN, and yes it used for inter process sync.
Yes, that's no problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.