Frage

It's known that, in cases when one needs comunicate between UI thread and working thread, an hidden window must be created because of thread safety(handle reconstruction).

For exemplify:

  • Form1 has N dynamicaly created TProgressBar instances with the same name of a background running .
  • Is always garanteed that WM_REFRESH will only be called inside Task Thread.
  • Form1 has H : THandle property that allocates the following procedure:

    procedure RefreshStat(var Message: TMessage); message WM_REFRESH;

  • Inside RefreshStat, in cases when there is only 1 background thread I could easily use L and W parameter to map Task Id and position.

I don't know if the title says what I want to know, but let's imagine if we have an application that has multiple background tasks running.
In my case I use TProgressBar to report progress the done.

Does AllocateHwnd garantee that all messages arrives with no race condition the hidden window?
What happens if two or more tasks post the message at the same time?

If this needs to be controled manually, I wonder if there is something else to do besides creating another message loop system in the custom message.

I hope the question is clear enough.

War es hilfreich?

Lösung

The message queue associated with a thread is a threadsafe queue. Both synchronous and asynchronous messages from multiple other thread are delivered safely no harmful date races. There is no need for any external synchronization when calling the Windows message API functions like SendMessage and PostMessage.

If two threads post or send messages to the same window at the same time, then there is no guarantee as to which message will be processed first. This is what is known as a benign race condition. If you want one message to be processed before the other then you must impose an ordering.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top