Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Terminated property of thread is set?

I have multithread application.

procedure TGridUpdater.Execute;
begin
  inherited;
  CodeSite.Send('Thread executed');
  sp := ConnectionFactory.GetConnection.LoadStoredProc('rab_itemData');
  sp.CreateParam('Tahun', ftInteger, sppdInput).Value := _form.tahun;
  sp.Open;
  Synchronize(DoProgress1);
  sp.DataSet.First;
  I := 0;
  CodeSite.Send('Terminated value is ' + BoolToStr(Terminated)); //The terminated property is 'True' here. Why?

  while (not Terminated) and (not sp.DataSet.Eof) do
  begin
    CodeSite.Send('Entering loop');
    Synchronize(DoProgress);
    I := I + 1;
    sp.DataSet.Next;
  end;
  Synchronize(DoProgress2);

end;

Method to run thread

procedure TFRABData.RefreshDataset;
var
  GridUpdater: TGridUpdater;
begin
  GridUpdater := TGridUpdater.Create(True);
  GridUpdater.OwnerForm := Self;
  GridUpdater.Start;
  CodeSite.Send('RefreshDataset executed');
  GridUpdater.Free;
end;
like image 405
Niyoko Avatar asked Dec 27 '22 17:12

Niyoko


1 Answers

You are freeing your thread immediately after you start it. You have to let it live its own life. So to answer your question, it's the thread's destructor that sets the Terminated flag to True.

procedure TFRABData.RefreshDataset;
var
  GridUpdater: TGridUpdater;
begin
  GridUpdater := TGridUpdater.Create(True);
  GridUpdater.OwnerForm := Self;
  GridUpdater.Start;
  CodeSite.Send('RefreshDataset executed');
  GridUpdater.Free; // <-- don't do this; let the thread live its own life
end;
like image 166
TLama Avatar answered Jan 08 '23 08:01

TLama