繁体   English   中英

Delphi 2007 中的 IdThreadComponent (Indy 9) 错误

[英]IdThreadComponent (Indy 9) in Delphi 2007 Error

我正在使用 IdTCPClient 和 IdThreadComponent 来获取条形码阅读器的一些信息。 此代码在 Delphi 11 和 Indy 10 中有效,但在 Delphi 2007 和 Indy 9 中无效:

procedure TPkgSendF1.IdThreadComponent1Run(Sender: TIdCustomThreadComponent);
var
  s: String;
begin
  s := IdTCPClient1.ReadLn('&', 20000, 1500);
  TThread.Queue(nil, procedure  // <== Expected @ but received PROCEDURE
                begin
                  ProcessRead(s);
                end);
end;

// [DCC Error] PkgSendF1.pas(239): E2029 Expression expected but 'PROCEDURE' found

procedure TPkgSendF1.ProcessRead(AValue: string);
begin
  Memo1.Text := AValue;
end;

如果我不使用 TThread.Queue 我会错过一些读数。 我会很感激任何帮助。 弗朗西斯科·阿尔瓦拉多

Delphi 2007 中尚不存在匿名方法,它们是在 Delphi 2010 中引入的。因此,D2007 中的TThread.Queue()只有一个接受TThreadMethod的版本:

type
  TThreadMethod = procedure of object;

这意味着您需要将对ProcessRead()的调用包装在一个辅助对象中,该对象具有一个没有参数的procedure ,例如:

type
  TQueueHelper = class
  public
    Caller: TPkgSendF1;
    Value: String;
    procedure DoProcessing;
  end;

procedure TQueueHelper.DoProcessing;
begin
  try
    Caller.ProcessRead(Value);
  finally
    Free;
  end;
end;

procedure TPkgSendF1.IdThreadComponent1Run(Sender: TIdCustomThreadComponent);
var
  s: string;
begin
  s := IdTCPClient1.ReadLn('&', 20000, 1500);
  with TQueueHelper.Create do
  begin
    Caller := Self;
    Value := s;
    TThread.Queue(nil, DoProcessing);
  end;
end;

仅供参考,Indy(9 和 10)在IdSync单元中有一个异步TIdNotify类,您可以使用它而不是直接使用TThread.Queue() ,例如:

uses
  IdSync;

type
  TMyNotify = class(TIdNotify)
  public
    Caller: TPkgSendF1;
    Value: String;
    procedure DoNotify; override;
  end;

procedure TMyNotify.DoNotify;
begin
  Caller.ProcessRead(Value);
end;

procedure TPkgSendF1.IdThreadComponent1Run(Sender: TIdCustomThreadComponent);
var
  s: string;
begin
  s := IdTCPClient1.ReadLn('&', 20000, 1500);
  with TMyNotify.Create do
  begin
    Caller := Self;
    Value := s;
    Notify;
  end;
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM