0

I have a thread defined THttpThread for downloading of a file. I would like to stop the download if modal form is closed or if Cancel button is pressed.

In the example from bellow I got Access Violation probably because of the way how I reuse the thread.

procedure Tform_update.button_downloadClick(Sender: TObject);
var
  HttpThread: THttpThread;
begin
  //download
  if button_download.Tag = 0 then
    begin
      HttpThread:= THttpThread.Create(True);
      //...
      HttpThread.Start;
    end
  //cancel download
  else
    begin
      HttpThread.StopDownload:= True;
    end;
end;

I sow the answers from How stop (cancel) a download using TIdHTTP and some many others but I still don't get it how to update a property of a running thread.

Community
  • 1
  • 1
REALSOFO
  • 852
  • 9
  • 37
  • 4
    So, did you write THttpThread? If so, where is its code? – GolezTrol Jul 19 '15 at 20:24
  • THttpThread is written and it's working well when I press "Download" button. But when I press "Cancel" I got Acces Violation only by doing this `HttpThread.StopDownload:= True;` to the same running thread. – REALSOFO Jul 19 '15 at 20:28
  • 6
    You are getting the access violation because your `HttpThread` variable is not assigned when your click handler runs a second time to handle the "Cancel" action. You'll need to store the reference somewhere (e.g. in a field of the `Tform_update` class). – Phil Ross Jul 19 '15 at 20:34
  • Indeed, your variable is local to this procedure and cannot be seen from anywhere else. – Jerry Dodge Jul 19 '15 at 20:35
  • let's suppose I would like to start the thread under this button and then to update that property `HttpThread.StopDownload:= True;` in order to say to the thread to disconnect idHttp how can be done? Of course that If I want to close the modal form, `HttpThread: THttpThread;` must be defined under `Private` section. Remark: button Download is transforming in Cancel after download start... but I think is obvious. – REALSOFO Jul 19 '15 at 20:45
  • It sounds like you're looking for a thread pool. – Jerry Dodge Jul 19 '15 at 20:46
  • See this [link](http://delphi.about.com/od/kbthread/a/thread-gui.htm)... `aButton.OwnedThread := aThread;` ??? – REALSOFO Jul 19 '15 at 20:55
  • 3
    You must declare the HttpThread as a member of the Tform_update. That way you can safely access it both when you press cancel and when you close the form. But without seeing the code in your thread, it is difficult to write a proper answer, since the access of a thread from outside is depending on the details. – LU RD Jul 19 '15 at 21:42

1 Answers1

1

I will give the answer found, using also the hints from users comments.

The access violation comes from the fact HttpThread was not assigned during cancellation. Reason why HttpThread: THttpThread must be defined under his form like:

Tform_update = class(TForm)
//...
private
  HttpThread: THttpThread;

and then the code should be:

procedure Tform_update.button_downloadClick(Sender: TObject);
begin
  //download
  if button_download.Tag = 0 then
    begin
      HttpThread:= THttpThread.Create(True);
      //...
      HttpThread.Start
    end
  //cancel download
  else
    begin
      if Assigned(HttpThread) then HttpThread.StopDownload:= True;
    end;
end;

Same for form close

procedure Tform_update.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Assigned(HttpThread) then HttpThread.StopDownload:= True;
end;

As some users ask for in some comments, there is no need for the code of thread.

REALSOFO
  • 852
  • 9
  • 37
  • because inside thread is running idHttp, which must be disconnected first, and then thread will stop by itself. – REALSOFO Jul 21 '15 at 08:54