Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2023-07

TThread.Queue vs Application.QueueAsyncCall

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

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?

When you're not using Lazarus' TApplication obviously you cannot use QueueAsyncCall, but for a gui application which one is preferable?

Just curious, after having used "Synchronize" forever (the only option when I began using delphi, apart from PostMessage), I recently switched to QueueAsyncCall so that a thread can go on without waiting and now I discovered TThread.Queue.

Bye

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

Reply by email

Mattias Gaertner
to Luca Olivetti
Luca Olivetti wrote 7 lines
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.

Mattias

Reply by email

Luca Olivetti
to Mattias Gaertner

El 18/7/23 a les 13:50, Mattias Gaertner via lazarus ha escrit:

Show quoted text 18 lines
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 https://wiki.freepascal.org/Asynchronous_Calls#Record_passed_to_async_function

(**) 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.

Bye

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

Reply by email