我有多线程应用程序。
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;
运行线程的方法
procedure TFRABData.RefreshDataset;
var
GridUpdater: TGridUpdater;
begin
GridUpdater := TGridUpdater.Create(True);
GridUpdater.OwnerForm := Self;
GridUpdater.Start;
CodeSite.Send('RefreshDataset executed');
GridUpdater.Free;
end;
答案 0 :(得分:10)
启动后,您可以立即释放线程。你必须让它过自己的生活。所以为了回答你的问题,线程的析构函数将Terminated
标志设置为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;