等待线程,而不冻结应用程序 [英] Wait for thread without freezing the application

查看:114
本文介绍了等待线程,而不冻结应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个indy TIdHttp放在一个线程中,
我已经尝试过:

I'm trying to put an indy TIdHttp in a thread, I have tried this :

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;

主要内容:

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,但它冻结了应用程序。

My problem here is that the next instruction gets executed(showmessage) without waiting the thread to be done, i tried to use the "WaitFor" but it freezes the application.

还有其他解决方法吗?

Is there any other workaround?

谢谢。

推荐答案

使用 TThread.OnTerminate 事件知道线程何时完成:

Use the TThread.OnTerminate event to know when the thread has finished:

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您可以这样做:

If you want to use a loop to wait for the thread to finish, without blocking the UI, then you can do it like this:

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;

这篇关于等待线程,而不冻结应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆