How to pass message to TApartmentThread Instance without creating Windows handle

自作多情 提交于 2019-12-12 17:04:45

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/32322329/how-to-pass-message-to-tapartmentthread-instance-without-creating-windows-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!