问题描述
我正在这样使用TIdNotify:
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;
在Embarcadero Delphi Tokyo中进行编译时,我收到此消息:
[dcc64警告] Unit1.pas(230):不推荐使用W1000符号“ TIdNotify”:“使用静态TThread.Queue()”
为新版本的Delphi重新编码此代码的正确方法是什么?
解决方法
将TThread.Queue()
与匿名过程一起使用,例如:
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;