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
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?
You need to call Terminate()
, WaitFor()
and Free()
all together, eg:
procedure TFRABData.RefreshDataset;
var
GridUpdater: TGridUpdater;
begin
if Assigned(updaterThread) then
begin
updaterThread.Terminate;
updaterThread.WaitFor;
FreeAndNil(updaterThread);
end;
GridUpdater := TGridUpdater.Create(True);
GridUpdater.OwnerForm := Self;
GridUpdater.Start;
updaterThread := GridUpdater;
CodeSite.Send('RefreshDataset executed');
end;
And in addition to RRUZ's answer, to let it work with FreeOnTerminate = False
:
Terminate
just sets the flag, it does nothing more.
Change
if Assigned(updaterThread) and (updaterThread <> nil) then
begin
updaterThread.Terminate;
end;
to
if Assigned(updaterThread) then
begin
updaterThread.Free;
end;
Free
will call Terminate
and WaitFor
subsequently to eliminate your memory leak.
TThread
constructor to receive the OwnerForm parametersomething like so.
TGridUpdater = class(TThread)
private
FOwnerForm: TForm;
public
constructor Create(OwnerForm : TForm); overload;
destructor Destroy; override;
procedure Execute; override;
end;
constructor TGridUpdater.Create(OwnerForm: TForm);
begin
inherited Create(False);
FreeOnTerminate := True;
FOwnerForm:=OwnerForm;
end;
destructor TGridUpdater.Destroy;
begin
inherited;
end;
procedure TGridUpdater.Execute;
begin
//your code goes here
end;
Now you can create your Tthread on this way
GridUpdater:=TGridUpdater.Create(Self); //Just set it and forget it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With