Search code examples
multithreadingdelphidatasnap

How to pass message to TApartmentThread Instance without creating Windows handle


I wish to be able to post messages from the main application of a DataSnap automation server to the RemoteDataModule instances created to service clients.

I understand from other forum entires, such as:

Delphi Multi-Threading Message Loop

That messages can be handled in threads without the need to create a Windows handle for the purpose; messages posted with PostThreadMessage.

However the DataSnap TComponentFactory creates a TApartmentThread instance for each RemoteDataModule, and it's Execute method already processes messages:

    while GetMessage(msg, 0, 0, 0) do
    begin
      DispatchMessage(msg);
      Unk._AddRef;
      if Unk._Release = 1 then break;
    end;

I imagine to get this to process any custom message of mine, would mean re-writing the TApartmentThread Execute method.

I have confirmed that creating a window handle to process messages in a RemoteDataModule instance works using the technique described here:

http://delphi.about.com/od/windowsshellapi/a/receive-windows-messages-in-custom-delphi-class-nonwindowed-control.htm

This uses AllocateHWnd, which can be made thread-safe thanks to the work of others:

How can I make AllocateHwnd threadsafe?

Although this presents a solution, I would like to ask; is there a recommended alternative approach?


Solution

  • It would appear that the only other means of processing custom messages in a TApartmentThread instance would be to rewrite the Execute method.