本文介绍了为什么设置线程的终止属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多线程应用程序.

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;

运行线程的方法

procedure TFRABData.RefreshDataset;
var
  GridUpdater: TGridUpdater;
begin
  GridUpdater := TGridUpdater.Create(True);
  GridUpdater.OwnerForm := Self;
  GridUpdater.Start;
  CodeSite.Send('RefreshDataset executed');
  GridUpdater.Free;
end;

推荐答案

启动线程后,您将立即释放线程.您必须让它过自己的生活.因此,要回答您的问题,是由线程的析构函数来设置 Terminated 标记为True.

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;

这篇关于为什么设置线程的终止属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:59