[Lazarus] Enqueuing a callback from a thread via other class - or am I overcomplicating this ?

Michael Schnell mschnell at lumino.de
Mon Jun 19 13:37:42 CEST 2017


On 17.06.2017 22:25, el es via Lazarus wrote:
> Where does the call queued from a thread, return to ?
 From the POV of the application programmer: "nowhere". it's just 
another (main-Thread-) "Event" that (like "OnClick") gets "fired" by the 
Lazarus/fpc infrastructure and is done.
> The object's lifetime is controlled by the dedicated thread
> (meaning only one thread is allowed to Free() it);
Of course a queued event should not use an object that might get freed 
by other means.

If you want to pass data to the event, a useful way to avoid this is to 
create a transfer object:

Type TTransfer = class; procedure the_event; TData: the_data; .....

running in the WorkerThread:

Transfer := TTransfer.Create(...)
Transfer.the_data. ..... := ....
queue(@Transfer.the_event);

(no Transfer.Free in the thread !!!)



running in the MainThread:

procedure TTransfer.the_event()
begin
...
fetch something from the_data
...
Free;      (this is Actually Self.Free; )
end;


-Michael



More information about the Lazarus mailing list