my app have a problem. Longer run time = more MB in RAM. It start after i tryed to using Threads. Im using 3x Tread with timer. Threads im using because im using httpget, and without it GUI freeze always when it start call httpget that download file about 150 byte. Timer interval can be set at 5 - 300 seconds most of times is used 5s. So every timer time im calling 3x Thread and in each Thread im calling httpget. But app eating memory maybe im doing something wrong with threads? Maybe they stay "open" somehow?
var
a, b, c: string;
type
TThread_1 = class(TThread)
private
FVar_1: string;
procedure Update_1;
protected
procedure Execute; override;
end;
TThread_2 = class(TThread)
private
FVar_2: string;
procedure Update_2;
protected
procedure Execute; override;
end;
TThread_3 = class(TThread)
private
FVar_3: string;
procedure Update_3;
protected
procedure Execute; override;
end;
procedure TThread_1.Execute;
begin
FVar_1 := HttpGet('url', 'a');
Synchronize(Update_1);
end;
procedure TThread_1.Update_1;
begin
a := FVar_1;
end;
procedure TThread_2.Execute;
begin
FVar_2 := HttpGet('url', 'b');
Synchronize(Update_2);
end;
procedure TThread_2.Update_2;
begin
b := FVar_2;
end;
procedure TThread_3.Execute;
begin
FVar_3 := HttpGet('url', 'c');
Synchronize(Update_3);
end;
procedure TThread_3.Update_3;
begin
c := FVar_3;
end;
procedure TForm1.Timer_thread(Sender: TObject);
var
t1 : TThread_1;
t2 : TThread_2;
t3 : TThread_3;
begin
if lb_1.IsChecked = True then
begin
t1 := TThread_1.Create(true);
try
t1.FreeOnTerminate := true;
finally
t1.Start;
end;
end;
if lb_2.IsChecked = True then
begin
t2 := TThread_2.Create(true);
try
t2.FreeOnTerminate := true;
finally
t2.Start;
end;
end;
if lb_3.IsChecked = True then
begin
t3 := TThread_3.Create(true);
try
t3.FreeOnTerminate := true;
finally
t3.Start;
end;
end;
end;
function HttpGet(const url: string; const tip: string): string;
var
HTTP: TIdHTTp;
begin
HTTP := TIdHTTP.Create(nil);
HTTP.Request.UserAgent := 'Mozilla/6.0 (compatible; Delphi)';
try
try
Result:=http.Get(url);
except
on E : Exception do
begin
if Pos('Error resolving Address', E.Message) <> 0 then
begin
if tip = '1' then Form1.lb_1.Text:='1';
if tip = '2' then Form1.lb_2.Text:='2';
if tip = '3' then Form1.lb_3.Text:='2';
end;
end;
end;
finally
FreeAndNil (http);
end;
end;
Edited code but still eating memory (1MB / 1 minute - timer interval 5 seconds), if timer turn off it stop eat memory:
procedure TForm1.Timer_thread(Sender: TObject);
var
t1 : TThread;
t2 : TThread;
t3 : TThread;
begin
if lb_1.IsChecked = True then
begin
t1:= TThread.CreateAnonymousThread(
procedure
var s: string;
begin
s := HttpGet('url_a', 'a');
TThread.Synchronize(nil,
procedure
begin
a := s;
end);
end);
t1.Start;
end;
if lb_2.IsChecked = True then
begin
t2:= TThread.CreateAnonymousThread(
procedure
var s: string;
begin
s := HttpGet('url_b', 'b');
TThread.Synchronize(nil,
procedure
begin
b := s;
end);
end);
t2.Start;
end;
if lb_3.IsChecked = True then
begin
t3:= TThread.CreateAnonymousThread(
procedure
var s: string;
begin
s := HttpGet('c', 'c');
TThread.Synchronize(nil,
procedure
begin
c := s;
end);
end);
t3.Start;
end;
end;