我的应用在GET
事件上发出FormShow
命令,有时用户可能会按下Back
并在线程启动后关闭表单但尚未完成,从而导致错误例如Thread Error: Invalid argument (22)
或Thread Error: No such process (3)
。
procedure TForm58.FormShow(Sender: TObject);
begin
if Assigned(LListThread) then LListThread := nil;
LListThread := TLoadListThread.Create;
LListThread.OnTerminate := LoadListThreadTerminated;
LListThread.Start;
end;
constructor TLoadListThread.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
end;
procedure TLoadListThread.Execute;
begin
if Form58.IdHTTP1.Connected then Form58.IdHTTP1.Disconnect;
st := TStringList.Create;
try
ms := TMemoryStream.Create;
Synchronize(
procedure
begin
Form58.Label1.Text := 'Loading...';
end);
try
Form58.IdHTTP1.Get(urlserver,ms);
ms.Position := 0;
st.LoadFromStream(ms, TEncoding.UTF8);
finally
ms.Free;
end;
// Do something with st
finally
st.Free;
end;
end;
procedure TForm58.LoadListThreadTerminated(Sender: TObject);
begin
if IdHTTP1.Tag = 1 then
begin
LListThread := nil;
Form58.Close;
end else
begin
LListThread := nil;
if TThread(Sender).FatalException = nil then
// Do something
else
// Do otherthing
end;
end;
procedure TForm58.CloseButtonClick(Sender: TObject);
begin
if Assigned(LListThread) then
begin
IdHTTP1.Tag := 1;
LListThread.Terminate;
end else Form58.Close;
end;
procedure TForm58.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(LListThread) then
begin
LListThread.Destroy;
LListThread := nil;
end;
try
// Do something
finally
Action := TCloseAction.caFree;
end;
end;
我知道我制作的线程开始/结束逻辑很糟糕,如何改进它并删除显示的错误消息?
答案 0 :(得分:4)
当准备关闭表单时,如果线程仍然在运行,那么你发信号通知它终止,但是你没有等待它完全终止,然后你在表单中明确地销毁线程对象实际上即使您使用FreeOnTerminate=True
也会关闭。
在明确销毁线程对象之前,您应该调用TThread.WaitFor()
,但在使用FreeOnTerminate=True
时这不起作用,这会导致您看到的错误类型。除此之外,如果线程仍在运行,TThread
析构函数会自行调用WaitFor()
,并且您正在显式地销毁线程对象。无论哪种方式,都会导致错误。
所以,你需要:
设置FreeOnTerminate=False
,然后等待线程完全终止,然后再明确销毁它。
设置FreeOnTerminate=True
并且根本不要手动销毁线程对象,也不要关闭该表单直到线程终止。
我还建议覆盖线程的虚拟TerminatedSet()
方法来设置一个标志,然后可以在线程内检查以中止GET
请求,例如在{{1事件。
使用TIdHTTP.OnWork
时尝试这样的事情:
FreeOnTerminated=True
type
TLoadListThread = class(TThread)
private
FUrl: string;
FOnLoading: TNotifyEvent;
DoAbort: Boolean;
procedure CheckAbort;
procedure DoLoading;
procedure HTTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
protected
procedure TerminatedSet; override;
public
constructor Create(const AUrl: String);
property OnLoading: TNotifyEvent read FOnLoading write FOnLoading;
end;
constructor TLoadListThread.Create(const AUrl: String);
begin
inherited Create(True);
FreeOnTerminate := True;
FUrl := AUrl;
end;
procedure TLoadListThread.CheckAbort;
begin
if DoAbort then SysUtils.Abort;
end;
procedure TLoadListThread.DoLoading;
begin
if Assigned(FOnLoading) then FOnLoading(Self);
end;
procedure TLoadListThread.HTTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
CheckAbort;
end;
procedure TLoadListThread.TerminatedSet;
begin
inherited;
DoAbort := True;
end;
procedure TLoadListThread.Execute;
var
HTTP: TIdHTTP;
begin
HTTP := TIdHTTP.Create;
try
st := TStringList.Create;
try
ms := TMemoryStream.Create;
try
if Assigned(FOnLoading) then Synchronize(DoLoading);
CheckAbort;
HTTP.Get(FUrl, ms);
ms.Position := 0;
st.LoadFromStream(ms, TEncoding.UTF8);
finally
ms.Free;
end;
CheckAbort;
// Do something with st
finally
st.Free;
end;
finally
HTTP.Free;
end;
end;
或者,当使用private
procedure CloseOnTerminated(Sender: TObject);
procedure TForm58.FormShow(Sender: TObject);
begin
StopLoadListThread;
LListThread := TLoadListThread.Create(urlserver);
LListThread.OnLoading := LoadListThreadLoading;
LListThread.OnTerminate := LoadListThreadFinished;
LListThread.Start;
end;
procedure TForm58.StopLoadListThread;
begin
if Assigned(LListThread) then
begin
LListThread.OnLoading := nil;
LListThread.OnTerminate := nil;
LListThread.Terminate;
LListThread := nil;
end;
end;
procedure TForm58.LoadListThreadLoading(Sender: TObject);
begin
Label1.Text := 'Loading...';
end;
procedure TForm58.LoadListThreadFinished(Sender: TObject);
begin
if LListThread.FatalException = nil then
// Do something
else
// Do something else
LListThread := nil;
end;
procedure TForm58.CloseOnTerminated(Sender: TObject);
begin
LListThread := nil;
Close;
end;
procedure TForm58.CloseButtonClick(Sender: TObject);
begin
Close;
end;
procedure TForm58.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(LListThread) then
begin
LListThread.OnTerminate := CloseOnTerminated;
LListThread.Terminate;
Action := TCloseAction.caNone;
end
else
begin
// Do something
Action := TCloseAction.caFree;
end;
end;
时:
FreeOnTerminated=False
type
TLoadListThread = class(TThread)
private
FUrl: string;
FOnLoading: TNotifyEvent;
DoAbort: Boolean;
procedure CheckAbort;
procedure DoLoading;
procedure HTTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
protected
procedure TerminatedSet; override;
public
constructor Create(const AUrl: String);
property OnLoading: TNotifyEvent read FOnLoading write FOnLoading;
end;
constructor TLoadListThread.Create(const AUrl: String);
begin
inherited Create(True);
FreeOnTerminate := False;
FUrl := AUrl;
end;
procedure TLoadListThread.CheckAbort;
begin
if DoAbort then SysUtils.Abort;
end;
procedure TLoadListThread.DoLoading;
begin
if Assigned(FOnLoading) then FOnLoading(Self);
end;
procedure TLoadListThread.HTTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
CheckAbort;
end;
procedure TLoadListThread.TerminatedSet;
begin
inherited;
DoAbort := True;
end;
procedure TLoadListThread.Execute;
var
HTTP: TIdHTTP;
begin
HTTP := TIdHTTP.Create;
try
st := TStringList.Create;
try
ms := TMemoryStream.Create;
try
if Assigned(FOnLoading) then Synchronize(DoLoading);
CheckAbort;
HTTP.Get(FUrl, ms);
ms.Position := 0;
st.LoadFromStream(ms, TEncoding.UTF8);
finally
ms.Free;
end;
CheckAbort;
// Do something with st
finally
st.Free;
end;
finally
HTTP.Free;
end;
end;