[英]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.