本文介绍了如何使用线程Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

begin

I:= Listbox1.ItemIndex;

如果I& -1然后
begin
S:= Listbox1.Items [I];
IdHTTP1.ProxyParams.ProxyServer:= Fetch(S,':');
IdHTTP1.ProxyParams.ProxyPort:= StrToInt(S);

try

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

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

除了

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

IdHTTP1.Disconnect; //ÖLDÜR。
结束
结束
结束

程序TForm1.Timer1Timer(Sender:TObject);
开始
Timer1.Enabled:= False;
try
ListBox1Click(nil);
如果ListBox1.ItemIndex< ListBox1.Items.Count - 1 then
ListBox1.ItemIndex:= ListBox1.ItemIndex + 1
else
ListBox1.ItemIndex:= -1;
finally
Timer1.Enabled:= True;
结束

如果ListBox1.ItemIndex = -1然后
Timer1.Enabled:= false;

end;


程序TForm1.BitBtn2Click(Sender:TObject);
begin
Timer1.Enabled:= true;
结束

提前谢谢。

解决方案

这是一个线程示例:

 键入
TMyThread = class(TThread)
protected
procedure Execute;覆盖
public:
ProxyIndex:Integer;
ProxyServer:String;
ProxyPort:TIdPort;
Url:String;
ReadTimeout:Integer;
属性ReturnValue;
结束

程序TMyThread.Execute;
var
IdHTTP:TIdHTTP;
开始
如果终止然后退出;
IdHTTP:= TIdHTTP.Create(nil);
try
IdHTTP.ProxyParams.ProxyServer:= ProxyServer;
IdHTTP.ProxyParams.ProxyPort:= ProxyPort;
IdHTTP.ReadTimeout:= ReadTimeout;
IdHTTP.Get(Url);
ReturnValue:= 1;
finally
IdHTTP.Free;
结束
结束

  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ı。)))) ;

如果CheckingAllProxies然后
begin
如果不是CheckProxy(LThread.ProxyIndex + 1)然后
CheckingAllProxies:= False;
结束
结束

函数TForm1.CheckProxy(ItemIndex:Integer):Boolean;
var
S:String;
LThread:TMyThread;
begin
结果:= False;
if(ItemIndex< 0)或(ItemIndex> = ListBox1.Items.Count)然后退出;
如果ListBox1.Items.Objects [ItemIndex]<>零退出;
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;
除了
LThread.Free;
加注
结束
LThread.Resume;
结果:= True;
结束

procedure TForm1.ListBox1Click(Sender:TObject);
begin
如果没有CheckingAllProxies然后
CheckProxy(ListBox1.ItemIndex);
结束

程序TForm1.BitBtn2Click(发件人:TObject);
begin
如果没有CheckingAllProxies然后
CheckingAllProxies:= CheckProxy(0);
结束

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


I work for a few days of this application. Form freezes until the entire transaction. How do I use 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;

Thank you in advance.

解决方案

Here is a threaded example:

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;

这篇关于如何使用线程Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:57