[Lazarus] LCL messaging system SendMessage and Multi-Threads

dmitry boyarintsev skalogryz.lists at gmail.com
Fri May 7 06:25:47 CEST 2010


On Fri, May 7, 2010 at 7:49 AM, Andrew Brunner
<andrew.t.brunner at gmail.com> wrote:
> I'm having a problem with a thread I have that is using SendMessage to
> post to a window handle (Ubuntu x64 10.04).  The message comes in just
> fine.  The problem is that I want to change the visibility of a
> component on a form and the program is closing out on
> ComponentName.Visible:=True;  Is there a work around to the messaging
> system to make them execute under the Application thread?

There's no equivalent for Window's SendMessage() in other systems.
But you can replace it with PostMessage + TEvent.WaitFor, something like:

TWorkThread
  procedure  Execute;
public
  WaitEvent : TEvent;
end;

procedure TWorkThread.Execute
begin
  WaitEvent.Reset;
  PostMessage(MainThread, WM_USER, Data, WaitEvent);
  WaitEvent.Wait(INFINITE);
end;

procedure TMainThread.WndProc(var msg, Data, WaitEvent: TEvent);
begin
  case msg of
    WM_USER: begin
       ProcessData;
       WaitEvent.SetEvent;
    end;
end;

However, there's a problem. If the main thread processing loop is
finished before processing all Sent messages from another thread, the
waiting messages will hang. On windows, they just stop waiting for the
sent message.

But according to your task you need to use Synchronize method. (see
FPC docs for the methods description)

thanks,
dmitry




More information about the Lazarus mailing list