Delphi, Tthread.Que. What is it? When should it be used?

Asked

Viewed 3,212 times

8

I was looking at 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 class inherited from TThread, TThread.Queue is indicated for which cases?

1 answer

9


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 parameter aMethod run using the main segment(main thread), thus avoiding conflicts between multiple threads. To thread is passed by the current parameter AThread.

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

The image below illustrates how this is done.

Image credit: huddled.with

TThread.Queue is comparable to the method Synchronize but with a single exception, in relation to thread current, when using the Synchronize to thread is suspended until the method is executed in the main segment, on the other hand, when using TThread.Queue the execution of thread is permitted to continue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.