我写了一个简单的程序,定期检查数据。问题是检查过程随着时间的推移而变慢。首先,它在一秒钟内发送两个帖子,大约45秒后,它发送一个帖子,最终它完全停止。我不知道这里怎么了。
procedure TForm2.CheckURLs;
var
lHTTP: TIdHTTP;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
N: Integer;
Access: string;
Params, Reply: TStringList;
Cookie: TIdCookie;
begin
lHTTP := TIdHTTP.Create(nil);
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmClient;
lHTTP.IOHandler := IdSSL;
lHTTP.ReadTimeout := 30000;
lHTTP.HandleRedirects := True;
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.Get('https://www.mywebserver.com', TStream(nil));
Cookie := lHTTP.CookieManager.CookieCollection.Cookie['id', 'www.mywebserver.com'];
if Cookie <> nil then
Access := Cookie.Value;
finally
end;
Params := TStringList.Create;
Reply := TStringList.Create;
TTask.Create(
procedure
var
N, m, i : integer;
begin
Params.Add('IDs=' +B.Strings[i]);
try
lHTTP.Request.CustomHeaders.Values['id'] := Access;
lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
lHTTP.Request.Referer := 'https://www.mywebserver.com/';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.ConnectTimeout := 0;
Reply.Text := lHTTP.Post('https://www.mywebserver.com/', Params);
if AnsiContainsStr(Reply.Text, 'nothing') then
begin
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add(B.Strings[i]);
Label2.Caption := Memo1.Lines.Count.ToString;
end
);
end;
finally
Reply.Clear;
end;
end
).Start;
end;
最佳答案
除了Technicolor所说的,Delphi 10.1 Berlin中的TTask
还存在时间问题,例如:
RSP-15233: TTask Stuck and take a very very long time to start
RSP-12557: TTask serialize parallel tasks without apparent reason
要避免这些问题,请尝试使用TThread.CreateAnonymousThread()
而不是TTask
。