等待线程而不冻结应用程序

7

我试图将一个Indy TIdHttp放在一个线程中,我尝试了以下代码:

type
  TSendThread = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  public
    http : TIdHTTP;
    URL : String;
    Method : String;
    property ReturnValue;
  end;

procedure TSendThread.Execute;
begin
  form1.Memo1.lines.Add(http.Get(URL));
  ReturnValue := 1;
end;

在主要部分:<\/p>
procedure TForm1.Button1Click(Sender: TObject);
var t : TSendThread;
begin
  t := TSendThread.Create(true);
  t.URL := 'http://www.url.com/';
  t.http := http;
  t.Start;
  showmessage(IntToStr(t.ReturnValue));
end;

我的问题在于下一个指令(showmessage)在线程完成之前就被执行了,我尝试使用“WaitFor”,但它会冻结应用程序。

是否有其他解决方法?

谢谢。


3
如果你只是在“等待它”完成,那么一开始就不需要将它放在线程中。线程的作用是在后台执行任务,这样UI界面就不必等待。 - Ken White
@Benjamin:如果你必须使用ProcessMessages,那么你现在又增加了一个新问题。这绝不是正确的解决方案。 - Ken White
使用谨慎,ProcessMessages 是可以的。 - OnTheFly
@OnTheFly-ProcessMessages 可能会导致意外行为。特别是在使用 TTimer 的冻结应用程序中使用时。 - Gabriel
1个回答

12
使用TThread.OnTerminate事件来知道线程何时完成:
type
  TSendThread = class(TThread)
  private
    http : TIdHTTP;
    Line: string;
    procedure AddLine;
  protected
    procedure Execute; override;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;
    URL : String;
    Method : String;
    property ReturnValue;
  end;

constructor TSendThread.Create;
begin
  inherited Create(True);
  FreeOnTerminate := True;
  http := TIdHTTP.Create;
end;

destructor TSendThread.Destroy;
begin
  http.Free;
  inherited;
end;

procedure TSendThread.Execute;
begin
  Line := http.Get(URL);
  Synchronize(AddLine);
  ReturnValue := 1;
end;

procedure TSendThread.AddLine;
begin
  Form1.Memo1.Lines.Add(Line);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TSendThread;
begin
  t := TSendThread.Create;
  t.URL := 'http://www.url.com/';
  t.OnTerminate := ThreadTerminated;
  t.Start;
end;

procedure TForm1.ThreadTerminated(Sender: TObject);
begin
  ShowMessage(IntToStr(TSendThread(Sender).ReturnValue));
end;

如果你想使用循环等待线程完成,而不阻塞UI,则可以像这样操作:

constructor TSendThread.Create;
begin
  inherited Create(True);
  //FreeOnTerminate := True; // <-- remove this
  http := TIdHTTP.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TSendThread;
  h : THandle;
begin
  t := TSendThread.Create;
  try
    t.URL := 'http://www.url.com/';
    t.Start;
    h := t.Handle;
    repeat
      case MsgWaitForMultipleObjects(1, h, 0, INFINITE, QS_ALLINPUT) of
        WAIT_OBJECT_0:   Break;
        WAIT_OBJECT_0+1: Application.ProcessMessages;
        WAIT_FAILED:     RaiseLastOSError;
      else
        Break;
      end;
    until False;
    ShowMessage(IntToStr(t.ReturnValue));
  finally
    t.Free;
  end;
end;

是的,这也是我所做的:)。非常感谢您,我已经寻找了2天。 - Ouerghi Yassine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接