I have a rather simple multi-threaded VCL gui application written with Delphi 2007. I do some processing in multiple child threads (up to 16 concurrent) that need to update
While I'm sure there is a right way and a wrong way. I've written code using both methods and the one I keep returning to is the SendMessage method and I'm not sure why.
The use of the SendMessage vs Synchronize doesn't really make any difference. Both work essentially the same. I think the reason I keep using SendMessage is that I perceive a greater amount of control, but I don't know.
The SendMessage routine causes the calling thread to pause and wait until the destination window finishes processing the message sent. Because of this the main app thread is essentially syncronized to the calling child thread for the duration of the call. You don't need to use a critical section in the windows message handler.
The data transfer is essentially one way, from the calling thread to the main application thread. You can return an integer type value in the message.result but nothing that points to a memory object in the main thread.
Since the two threads are "sync'd" that that point and the main apps thread is currently tied up responding to the SendMessage then you also don't need to worry about other threads coming in and trashing your data at the same time. So no you don't need to worry about using Critical Sections or other types of thread safety measures.
For simple things you can define a single message (wm_threadmsg1) and use the wparam and lparam fields to transfer (integer) status messages back and forth. For more complex examples you can pass a string by passing it via the lparam and typecasting it back to a longint. A-la longint(pchar(myvar)) or use pwidechar if your using D2009 or newer.
If you've already got it working with the Synchronize methods then I wouldn't worry about reworking it to make a change.