How do I make a pool
of Thread
, I need to execute a process that contains several records, but I need to send on demand, send 10 and as I release it, send more ... how can I do it? / p>
I've set an example ...
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
MinhaThread = class(TThread)
procedure Execute; override;
procedure Verifica;
procedure Fechar;
Private
constructor Create();
end;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
thread: MinhaThread;
public
{ Public declarations }
procedure consultaProcesso(Sender: TObject);
procedure postJSON(JSON:String);
end;
var
Form1: TForm1;
I : Integer;
JSON:String;
implementation
{$R *.dfm}
{ MinhaThread }
constructor MinhaThread.Create;
begin
inherited
Create(True);
FreeOnTerminate := True;
Priority := tpLower;
Resume;
end;
procedure MinhaThread.Execute;
Var Sender : TObject;
begin
Synchronize(Verifica);
Form1.consultaProcesso(Sender); // Executar Rotina ( Procedures )
while not Terminated do
begin
Sleep (10);
Terminate; // Finaliza a Thread
Synchronize(Fechar);
end;
end;
procedure MinhaThread.Fechar;
begin
//application.terminate;
end;
procedure MinhaThread.Verifica;
begin
Form1.Caption := 'EXECUTANDO...'+IntToStr(I);
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
thread := MinhaThread.Create();
end;
procedure TForm1.consultaProcesso(Sender: TObject);
begin
//exemplo com o for
// porem aqui eu percorro a query, passo
// para a variavel JSON o json que está na query
// e chamo o metodo post
for I := 0 to 100 do
begin
postJSON(JSON);
end;
end;
procedure TForm1.postJSON(JSON: String);
begin
//faz um post pelo idHTTP;
Memo1.Lines.Add(DateTimeToStr(now)+ ' - Executando JSON '+IntToStr(I));
//para simular um tempo de espera do retorno
Sleep(1000);
//retorno...
Memo1.Lines.Add(DateTimeToStr(now)+ ' - Retorno JSON '+IntToStr(I));
end;
end.
In the consultaProcesso
method, I put a for
, but it would be the same as the while
I do in query
by taking JSON
and passing to method postJSON
, in this method I put sleep
to simulate a return time that I have from post
done by idHTTP
.