在TVirtualStringTree中显示文件系统树数据

时间:2015-11-11 05:32:50

标签: multithreading delphi virtualtreeview tvirtualstringtree

我对文件系统对象有这样的线程安全类:

type
  PFSObject = ^TFSObject;
  TFSObject = class
  private
    FMREW: TMREWSync;

    FChildren: TObjectList<TFSObject>;
    FFilesCount: UInt32;
    FFoldersCount: UInt32;
    FName: string;
    FParent: TFSObject;

    function GetFullPath: string;
  public
    constructor Create(const AName: string; AParent: TFSObject; AFilesCount, AFoldersCount: UInt32 = 0);
    destructor Destroy; override;

    property Children: TObjectList<TFSObject> read FChildren write FChildren;
    property FilesCount: UInt32 read FFilesCount write FFilesCount;
    property FoldersCount: UInt32 read FFoldersCount write FFoldersCount;
    property Name: string read FName write FName;
    property Parent: TFSObject read FParent write FParent;

    procedure LockRead;
    procedure LockWrite;
    procedure UnlockRead;
    procedure UnlockWrite;
  end;

有线程,扫描文件系统并填写此内容。 在主窗体上有Timer,它接收来自此类的数据以在TVirtualStringTree中显示。

哪个是在TVirtualStringTree中显示此类数据的最佳方法,而不会丢失额外的内存来存储节点中的数据副本?

更新: 好的,我现在拥有的。

type
  PSizeData = ^TSizeData;
  TSizeData = record
    FSObj: PFSObject;
  end;

// OnTimer reader
procedure TformSize.tmrSizeTimer(Sender: TObject);
begin
  if tvSize.RootNodeCount = 0 then
    tvSize.RootNodeCount := 1
  else begin
    tvSize.Repaint;
    if FSThread.Finished then begin
      // Thread finished, disable timer
      SetTimerEnabled(False);
      // Expant first node
      tvSize.Expanded[tvSize.GetFirst] := True;
    end;
  end;
end;

// GetText of TVirtualStringTree
procedure TformSize.tvSizeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType; var CellText: string);
var
  Data, ParData: PSizeData;
begin
  // Check that children count changed for node
  Data := tvSize.GetNodeData(Node);
  if (Int32(Node.ChildCount) <> Data.FSObj.Children.Count) then begin
    tvSize.ChildCount[Node] := Data.FSObj.Children.Count;
    // Check that children count changed for parent node
    ParData := tvSize.GetNodeData(Node.Parent); 
    if Assigned(ParData) and (Int32(Node.Parent.ChildCount) <> ParData.FSObj.Children.Count) then
      tvSize.ChildCount[Node.Parent] := ParData.FSObj.Children.Count;
  end;
  // Get node text
  CellText := GetSizeDataText(Data, Column);
end;

// InitNode of TVirtualStringTree
procedure TformSize.tvSizeInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
  var InitialStates: TVirtualNodeInitStates);
var
  Data, ParData: PSizeData;
  PFSObj: PFSObject;
begin
  Data  := Sender.GetNodeData(Node);
  if not Assigned(ParentNode) then
    PFSObj := @FSThread.FSObject
  else begin
    ParData := Sender.GetNodeData(ParentNode);
    PFSObj := PFSObject(ParData.FSObj.Children[Node.Index]);
  end;
  Data.FSObj := PFSObj;
end;

现在我在TVirtualStringTree内存不足:( 我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

确定VCL主线程和文件读/写线程之间存在线程问题。

使用VCL时(请记住:TVirtualStringTree是VCL组件),您需要将其他线程与VCL主线程同步。

我过去为避免这些问题所做的工作:

  1. 在VCL主线程中创建一个互斥锁(也就是你的TForm或其他东西)
  2. 创建线程并将互斥锁传递给它
  3. 运行该线程,在访问VCL属性之前执行互斥锁定,然后执行互斥锁解锁
  4. 在VCL主线程中执行互斥锁定/解锁
  5. 基本上,如果没有安全同步,您永远不应该从其他线程访问或更改VCL属性。