Example of thread Pool in delphi

As I do a pool of Thread, I need to run a process that contains several records, but I need to send on demand, send 10 and as it is releasing, it will send more.... how can I do it ?

I put together 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 method consultaProcesso I put a for, but it would be the same as the while I do in the query taking the JSON and passing to the method postJSON, in this method I put a sleep to simulate a return time I have of the post performed by idHTTP.

Author: David, 2017-06-13

1 answers

See if this process helps solve your problem:

var
  MinhasThreads: Array of MinhaThread;
  i: Integer;
begin

  SetLength(MinhasThreads, 0);

  // Criando as threads dinâmicamente
  for i := 0 to 9 do
    begin
      SetLength(MinhasThreads, Length(MinhasThreads) + 1);
      MinhasThreads[High(MinhasThreads)] := MinhaThread.Create(True);
      MinhasThreads[High(MinhasThreads)].FreeOnTerminate := True;
      MinhasThreads[High(MinhasThreads)].Start;
    end;

  // Verificar se existe alguma Thread em execução
  //O sistema irá ficar rodando esse laço de repetição até que todas as threads sejam finalizadas.
  i := 0;
  while (i <= High(MinhasThreads)) do
    begin
      if (MinhasThreads[i] <> nil) then
        i := 0
      else
        Inc(i);
    end;
end;

The idea is, create all threads within an array, these threads are set to self-destruct as soon as the procedure execute is finished.

 2
Author: Roberto de Campos, 2017-06-13 18:25:50