1

I am using TIdNotify like this:

type
  TMsgNotify = class(TIdNotify)
  protected
    aMsgStr: string;
    procedure DoNotify; override;
  end;

procedure TMsgNotify.DoNotify;
begin
  // this runs in the main thread
  Form1.Log(aMsgStr); 
end;

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  try
    ...    
    if ARequestInfo.Command = 'POST' then
      begin
        //do Msg thread
        with TMsgNotify.Create do begin
          aMsgStr:='>> RawHTTP Command: ' + ARequestInfo.RawHTTPCommand;
          Notify;
          // Do not call Free(), TIdNotify will free itself when it is finished...
        end;
        ...
      end;
  finally
    ...
  end;
end;

When compiled in Embarcadero Delphi Tokyo I get this message:

[dcc64 Warning] Unit1.pas(230): W1000 Symbol 'TIdNotify' is deprecated: 'Use static TThread.Queue()'

What is proper way to recode this for new version of Delphi?

davornik
  • 81
  • 6

1 Answers1

1

Use TThread.Queue() with an anonymous procedure, eg:

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  aMsgStr: string;
begin
  try
    ...
    if ARequestInfo.Command = 'POST' then
    begin
      //do Msg thread
      aMsgStr := '>> RawHTTP Command: ' + ARequestInfo.RawHTTPCommand;
      TThread.Queue(nil,
        procedure
        begin
          // this runs in the main thread
          Log(aMsgStr);
        end
      );
      ...
    end;
  finally
    ...
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Does TThread.Queue needs to be with TThread.Queue(TThread.Current, ... ); or nil is just ok? – davornik Sep 28 '20 at 18:47
  • 1
    Use `nil` in this example. If you pass in a `TThread` object, the queued action will be associated with that thread and will be cancelled if that thread terminates before the main thread has processed the action. If you pass in `nil` instead, the queued action will not be associated with any thread, and will continue to be processed by the main thread even if the queuing thread terminates first. IOW, rule of thumb - pass in a `TThread` object only if the queued action relies on data that will become invalid if the queuing thread terminates, otherwise pass in `nil`. – Remy Lebeau Sep 28 '20 at 19:16