如何使用线程Delphi

时间:2012-07-18 18:25:30

标签: multithreading delphi listbox

我在这个应用程序中工作了几天。表格冻结直到整个交易。我如何使用Tread?

procedure TForm1.ListBox1Click(Sender: TObject);
var
  I: Integer;
  S: String;

  begin

   I := Listbox1.ItemIndex;

    if I <> -1 then
    begin
    S := Listbox1.Items[I];
    IdHTTP1.ProxyParams.ProxyServer := Fetch(S, ':');
    IdHTTP1.ProxyParams.ProxyPort := StrToInt(S);

    try

    IdHTTP1.ReadTimeout:=strtoint(form1.ComboBox1.Text); // ZMAAN AŞIMI
   IdHTTP1.Get(Edit4.Text);                         // POST GET

  Memo1.Lines.Add(Format('Sıra %d %s', [I, 'Bağlandı.']));

 except

 Memo1.Lines.Add(Format('Sıra %d %s', [I, 'Bağlanamadı.']));

IdHTTP1.Disconnect;   // ÖLDÜR.
end;
end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   Timer1.Enabled := False;
  try
    ListBox1Click(nil);
    if ListBox1.ItemIndex < ListBox1.Items.Count - 1 then
      ListBox1.ItemIndex := ListBox1.ItemIndex + 1
    else
      ListBox1.ItemIndex := -1;
   finally
    Timer1.Enabled := True;
  end;

  if ListBox1.ItemIndex = -1 then
Timer1.Enabled:=false;

end;


procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  Timer1.Enabled := true;
end;

提前谢谢。

1 个答案:

答案 0 :(得分:5)

这是一个线程示例:

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

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

var
  CheckingAllProxies: Boolean = False;

procedure TForm1.ThreadTerminated(Sender: TObject);
var
  LThread: TMyThread;
begin
  LThread := TMyThread(Sender);

  ListBox1.Items.Objects[LThread.ProxyIndex] := nil;

  Memo1.Lines.Add(Format('Sıra %d %s', [LThread.ProxyIndex, iif(LThread.ReturnValue = 1, 'Bağlandı.', 'Bağlanamadı.')])); 

  if CheckingAllProxies then
  begin
    if not CheckProxy(LThread.ProxyIndex + 1) then
      CheckingAllProxies := False;
  end;
end;

function TForm1.CheckProxy(ItemIndex: Integer): Boolean; 
var 
  S: String; 
  LThread: TMyThread;
begin 
  Result := False;
  if (ItemIndex < 0) or (ItemIndex >= ListBox1.Items.Count) then Exit;
  if ListBox1.Items.Objects[ItemIndex] <> nil then Exit;
  S := ListBox1.Items[ItemIndex]; 
  LThread := TMyThread.Create(True);
  try
    LThread.ProxyIndex := ItemIndex;
    LThread.ProxyServer := Fetch(S, ':'); 
    LThread.ProxyPort := StrToInt(S); 
    LThread.Url := Edit4.Text;
    LThread.ReadTimeout := StrToInt(ComboBox1.Text);
    LThread.OnTerminate := ThreadTerminated;
    LThread.FreeOnTerminate := True;
    ListBox1.Items.Objects[ItemIndex] := LThread;
  except
    LThread.Free;
    raise;
  end;
  LThread.Resume;
  Result := True;
end; 

procedure TForm1.ListBox1Click(Sender: TObject); 
begin
  if not CheckingAllProxies then
    CheckProxy(ListBox1.ItemIndex);
end;

procedure TForm1.BitBtn2Click(Sender: TObject); 
begin 
  if not CheckingAllProxies then
    CheckingAllProxies := CheckProxy(0);
end; 

procedure TForm1.BitBtn3Click(Sender: TObject); 
begin 
  CheckingAllProxies := False;
end;