Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the <code>TThread.OnTerminate</code> event to know when the thread has finished:</p> <pre><code>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; </code></pre> <p></p> <pre><code>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; </code></pre> <p></p> <pre><code>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; </code></pre> <p>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:</p> <pre><code>constructor TSendThread.Create; begin inherited Create(True); //FreeOnTerminate := True; // &lt;-- remove this http := TIdHTTP.Create; end; </code></pre> <p></p> <pre><code>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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload