如何在delphi中释放线程 [英] How to free a thread in delphi
问题描述
我有多线程应用程序,请此处。
我想终止线程,并在调用以下方法时启动新的。
I have multithreaded application as I ask here. I want to terminate the thread, and start a new one when following method is called.
procedure TFRABData.RefreshDataset;
var
GridUpdater: TGridUpdater;
begin
if Assigned(updaterThread) and (updaterThread <> nil) then
begin
updaterThread.Terminate;
end;
GridUpdater := TGridUpdater.Create(True);
GridUpdater.OwnerForm := Self;
updaterThread := GridUpdater;
GridUpdater.FreeOnTerminate := False;
GridUpdater.Start;
CodeSite.Send('RefreshDataset executed');
end
但是,当 FreeOnTerminate
设置为 True
,我获得访问违例,但是当 FreeOnTerminate
设置为 False
,我得到内存泄漏。如何释放线程?
but, when FreeOnTerminate
set to True
, I get Access Violation, but when FreeOnTerminate
set to False
, I get memory leak. How to free the thread?
推荐答案
除了 RRUZ的答案,让它与 FreeOnTerminate = False
一起使用:
And in addition to RRUZ's answer, to let it work with FreeOnTerminate = False
:
终止
只是设置标志,它什么也没有。
Terminate
just sets the flag, it does nothing more.
更改
if Assigned(updaterThread) and (updaterThread <> nil) then
begin
updaterThread.Terminate;
end;
至
if Assigned(updaterThread) then
begin
updaterThread.Free;
end;
免费
将调用终止
和 WaitFor
,以消除您的内存泄漏。
Free
will call Terminate
and WaitFor
subsequently to eliminate your memory leak.
这篇关于如何在delphi中释放线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!