Ver Mensaje Individual
  #1  
Antiguo 06-06-2016
Ramsay Ramsay is offline
Miembro
NULL
 
Registrado: ene 2016
Posts: 104
Reputación: 9
Ramsay Va por buen camino
MultiThread cada 5 Thread

Hola , estoy usando multithread para comprobar paginas en un array largo , el tema es que funciona bien y todo , pero tendria que ser cada 10 o 5 Thread porque sino me consume mucho , me refiero a que cargue 5 thread y cuando terminen continue con otros 5 , el codigo de la unit de los threads.

Código Delphi [-]
unit ThreadsTest;

interface

uses Windows, Classes, IdHTTP;

type
  TMyThread = class(TThread)
  protected
    procedure Execute; override;
  public
    Url: String;
    ReadTimeout: Integer;
    property ReturnValue;
  end;

implementation

procedure TMyThread.Execute;
var
  IdHTTP: TIdHTTP;
begin
  if Terminated then
    Exit;
  IdHTTP := TIdHTTP.Create(nil);
  try
    IdHTTP.ReadTimeout := ReadTimeout;
    IdHTTP.Get(Url);
    ReturnValue := 1;
  finally
    IdHTTP.Free;
  end;
end;

end.

Form

Código Delphi [-]
procedure TForm1.ThreadTerminado(Sender: TObject);
var
  LThread: TMyThread;
begin
  LThread := TMyThread(Sender);
  Inc(index);
  Memo1.Lines.Add(IntToStr(index) + '-' + LThread.Url + '-' +
    IntToStr(LThread.ReturnValue));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  index: integer;
const
  paginas: array [1 .. 13] of string = ('http://localhost1', 'http://localhost',
    'http://localhost', 'http://localhost', 'http://localhost',
    'http://localhost', 'http://localhost', 'http://localhost',
    'http://localhost', 'http://localhost', 'http://localhost',
    'http://localhost', 'http://localhost');
begin
  index := 0;
  for i := Low(paginas) to High(paginas) do
  begin
    LThread := TMyThread.Create(True);
    try
      LThread.Url := paginas[i];
      LThread.ReadTimeout := 1000;
      LThread.OnTerminate := ThreadTerminado;
      LThread.FreeOnTerminate := True;
    except
      LThread.Free;
      raise;
    end;
    LThread.Resume;
  end;
end;

El array real es mas largo.

¿ Como deberia hacer esto ?
Responder Con Cita