2012-07-18 66 views
2

我工作了幾天這個應用程序。表格凍結直到整個交易。我如何使用胎面?如何使用線程Delphi

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

關於如何使用'TThread'有無數的例子和指南。請找到一個並將其作爲您的起點。如果/當你陷入困境時,你可能會有一個合理的問題。 – 2012-07-18 18:32:16

+1

我不懂英文。我覺得很困難。我有我的義務學校 – user1424940 2012-07-18 18:35:20

+0

這裏的Embarcadero線程示例:http://docwiki.embarcadero.com/CodeExamples/en/RTL.Threads_Sample – 2012-07-18 19:11:47

回答

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;