Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart Delphi TThread that lives the entire app lifetime

I have created a class that derives from TThread, because I wish to do some async stuff, however to avoid having to create another class, I built the entire thing around that thread class. Not sure if this is good practice or not, and if I cant get this to work, well then I suppose I have no choice but to recode..

The problem: I create the Thread on FormCreate, assign some properties, and I Free it on FormDestroy. In the Thread's constructor, I set FreeOnTerminate = False. When I click on a button on my Form, I Start(); the Thread. Okay, so it runs as expected, an error occurs (expected!), its being passed to my error handling event, and it appears to terminate. I then click the button again, and I get a Cannot call Start on a running or suspended thread error.

How can I finish the thread without freeing it, and enabling me to start it again?

like image 863
Jeff Avatar asked Jul 16 '11 20:07

Jeff


3 Answers

Catch the error in the thread, handle it there and then let the thread continue the work. To handle the error you could simply queue a method to the main thread to report the error, for example. I hope you aren't letting exceptions leave your thread Execute method.

like image 27
David Heffernan Avatar answered Oct 22 '22 15:10

David Heffernan


You can't restart a thread once it is finished/terminated. In that case you should just create a new instance if the thread again like you did in FormCreate.

like image 87
Lars Truijens Avatar answered Oct 22 '22 16:10

Lars Truijens


This is the way that I implement it:

procedure TAPIRequest.DoRequest;
begin
  FBusy := True;
  Resume;
end;


procedure TAPIRequest.Execute;
begin
  inherited;

  while not Terminated do begin
    HttpError := False;

    try
      Response := HTTP.Post(URL, Params);
      ParseResponse;

    except
      HttpError := True;
    end;

    if Assigned(OnResponse) then
      OnResponse();

    FBusy := False;
    Suspend;
  end;
end;
like image 2
Juanmi Sosso Avatar answered Oct 22 '22 15:10

Juanmi Sosso