Delphi, TThread.Queue. Which is? When should it be used?

8

I was seeing this method that makes use of thread:

procedure TFormClient.QueueLogMsg(const s: string);
begin
  TThread.Queue(nil,
    procedure
    begin
      LogMsg(s)
    end
  );
end;

What is this method TThread.Queue ?
Unlike the implementation of a property class of TThread , TThread.Queue is indicated for which cases?

    
asked by anonymous 07.06.2014 / 17:00

1 answer

7
procedure Queue(AMethod: TThreadMethod); overload;
procedure Queue(AThreadProc: TThreadProcedure); overload;
class procedure Queue(AThread: TThread; AMethod: TThreadMethod); overload;
class procedure Queue(AThread: TThread; AThreadProc: TThreadProcedure); overload;

Queue causes the call specified by the aMethod parameter to be executed using the main thread ( main thread ), thus avoiding conflicts between multiple threads . The current thread is passed by the AThread parameter.

TThread.Queue is recommended use in situations where you have no assertion if the method to be used is thread-safe .

The image below illustrates how this is done.

Imagecredit: < in> aviyehuda.com

TThread.Queue is comparable to the method Synchronize however, with a single exception , relative to the current thread , using the current Synchronize to thread method is suspended until the method runs on the main thread, in contrast, when using TThread.Queue the execution of the thread is allowed to continue.

    
07.06.2014 / 22:37