0

THttpThreadファイルのダウンロード用に定義されたスレッドがあります。モーダルフォームが閉じられた場合やキャンセルボタンが押された場合にダウンロードを中止したい。

以下の例では、おそらくスレッドを再利用する方法が原因で、アクセス違反が発生しました。

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;

TIdHTTP を使用してダウンロードを停止 (キャンセル) する方法と他の多くの方法から答えを見つけましたが、実行中のスレッドのプロパティを更新する方法はまだわかりません。

4

1 に答える 1

1

ユーザーのコメントからのヒントも使用して、見つかった答えを示します。

アクセス違反は、HttpThreadキャンセル中に割り当てられなかったという事実から発生します。理由HttpThread: THttpThreadは、彼のフォームの下で次のように定義する必要があります。

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

コードは次のようになります。

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;

フォームクローズも同様

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

一部のユーザーがいくつかのコメントで要求しているように、スレッドのコードは必要ありません。

于 2015-07-20T07:55:14.607 に答える