Delphi: 使用 IdHttp 和 Thread 停止下载
Delphi: Stop a download using IdHttp and Thread
我定义了一个线程 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;
我从 和许多其他人那里得到了答案,但我仍然不明白如何更新 运行 线程的 属性。
我会给出找到的答案,同时使用用户评论中的提示。
访问冲突是由于 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;
根据某些用户在某些评论中的要求,不需要thread的代码。
我定义了一个线程 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;
我从 和许多其他人那里得到了答案,但我仍然不明白如何更新 运行 线程的 属性。
我会给出找到的答案,同时使用用户评论中的提示。
访问冲突是由于 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;
根据某些用户在某些评论中的要求,不需要thread的代码。