[Lazarus] TThread.Queue vs Application.QueueAsyncCall

Luca Olivetti luca at wetron.es
Tue Jul 18 14:23:08 CEST 2023

El 18/7/23 a les 13:50, Mattias Gaertner via lazarus ha escrit:
> On 17.07.23 19:01, Luca Olivetti via lazarus wrote:
>> Hello,
>> TThread.Queue and Application.QueueAsyncCall more or less do the same 
>> thing (even if they use two different queues that are managed at 
>> different times in the main thread).
>> When it's better to use one or the other?
> Application.QueueAsyncCall has a Data: PtrInt parameter to pass some 
> context and it is always called later, so this is useful for doing 
> something after the current LCL event.
> TThread.Queue has aThread parameter, so the call is removed when the 
> thread is freed, and when you call it from the main thread it will 
> execute immediately, so this is for worker threads to asynchronously do 
> something in the main thread.

Yes, but they both allow the thread to run something asynchronously in 
the main thread (at different times but that's not really a concern).
Both put the method in a list and try to wake the main thread (if possible).
The main difference I see is that passing volatile data(*) is a little 
more convoluted with TThread.Queue(**) than with 
Application.QueueAsyncCall, but apart from that I see no big 
differences, that's why I'm asking when it's better to use one or the 
other (or if it doesn't really matter).

(*)  i.e. data that the thread can potentially modify *before* the main 
thread executes the method, but you want the main thread to see the data 
as it was when it was queued, like the example here 

(**) you need to create a class instance that stores the data and frees 
itself after executing the method.Maybe, due to the overhead of doing 
that, it's better to use QueueAsyncCall in these cases.

Luca Olivetti
Wetron Automation Technology https://wetron.es/
Tel. +34 93 5883004 (Ext.3010)  Fax +34 93 5883007

More information about the lazarus mailing list