在 Delphi XE8 中,我试图SetParameter
从 OmniThreadLibrary 将数组传递给 OTL 任务:
implementation
type
TCookie = record
Name: string;
Value: string;
ExpDate: string;
ModDate: string;
end;
TCookieArray = array of TCookie;
var
CurCookies: TCookieArray;
procedure TForm1.btnStartTaskClick(Sender: TObject);
begin
SetLength(CurCookies, 2);
CurCookies[0].Name := 'username';
CurCookies[0].Value := 'Paul';
CurCookies[1].Name := 'password';
CurCookies[1].Value := 'none';
FGetCookieDetailsTask := CreateTask(GetCookieEntries, 'GetCookieEntries')
.MonitorWith(OTLMonitor)
// Compiler complaint:
.SetParameter('CookiesArray', TOmniValue.FromArray<TCookieArray>(CurCookies))
.Run;
end;
编译器抱怨 SetParameter 行:
[dcc32 Error] Unit1.pas(310): E2010 Incompatible types:
'System.TArray<Unit1.TCookieArray>' and 'TCookieArray'
不幸的是,OTL 书中没有关于如何使用FromArray
inSetParameter
将数组传递给任务的示例。
那么如何做到这一点呢?
编辑:ba__friend 要求我在他的回答的评论中显示我的解决方案的源代码:
FGetCookieDetailsTask := CreateTask(GetCookieEntries, 'GetCookieEntries')
.MonitorWith(OTLMonitor)
// Now no compiler complaint:
.SetParameter('CookiesArray', CurCookies)
.Run;
procedure GetCookieEntries(const task: IOmniTask);
var
TaskCookies, HostCookies: TCookieArray;
begin
HostCookies := task.Param['CookiesArray'];
TaskCookies := Copy(HostCookies, 0, Length(HostCookies));