【问题标题】:Delphi DragDrop Component in ThreadsDelphi DragDrop 组件在线程中
【发布时间】:2011-07-18 20:45:30
【问题描述】:

我使用这个组件来处理拖放文件 http://melander.dk/delphi/dragdrop

unit DragThread;

interface

uses
  Classes,DragDrop, DropTarget,DragDropFile,Dialogs,SysUtils;

type
  TDragThread = class(TThread)
  private
    { Private declarations }
    ArraysLength : Integer;
    DragComponent : TDropFileTarget;
    DragArray,HashsArray : Array of string;
    Procedure FDArray;
    //Procedure FDHArray;
  protected
    procedure Execute; override;
  Public
    Constructor Create(Com: TDropFileTarget);
    Destructor Destroy; Override;
  end;

implementation

{ TDragThread }

Constructor TDragThread.Create(Com: TDropFileTarget);
begin
   inherited Create(True);
   DragComponent := Com;
end;

Destructor TDragThread.Destroy;
begin
  //DragComponent.Free;
end;

Procedure TDragThread.FDArray;
var
  A : Integer;
begin
  SetLength(DragArray,DragComponent.Files.Count);
  SetLength(HashsArray,DragComponent.Files.Count);

  ShowMessage(IntToStr(DragComponent.Files.Count)); // just working in the first time !!

for A := 0 to DragComponent.Files.Count -1 do begin
      DragArray[A] := DragComponent.Files[A];
      //ShowMessage(DragComponent.Files[A]);

   end;
  ArraysLength := DragComponent.Files.Count-1;
  //ShowMessage(DragComponent.Files[0]);
end;

procedure TDragThread.Execute;
begin
  { Place thread code here }
  FDArray;
end;

end.

Drop 进程只运行一次然后 DragComponent.Files.Count 永远为 0 的奇怪之处。!!

我就是这么称呼它的

procedure TForm1.DropFileDrop(Sender: TObject; ShiftState: TShiftState;
  APoint: TPoint; var Effect: Integer);
var
  DropThread : TDragThread;
begin
 DropThread := TDragThread.Create(DropFile);
 DropThread.Resume;
end;

我想知道为什么会这样,并提前感谢:)。

【问题讨论】:

  • 它是否有助于您在主线程中从DragComponent 读取文件名并将处理留给工作线程?我没有这个拖放组件的经验,所以我不知道它的线程规则是什么。

标签: multithreading delphi drag-and-drop delphi-2009


【解决方案1】:

不要从其他线程操作 VCL 组件。

不保证一旦放置事件完成,组件的放置事件信息将继续有效。

在构造线程时将所需的所有信息从组件中复制出来(即完全填充DragArray),然后在执行线程时使用缓存的数据。不要将引用存储在 DragComponent 中,否则您可能会想从线程的 Execute 方法中使用它,而您确实不应该这样做。

【讨论】:

  • 非常感谢 Rob,你是对的,无法保证信息仍然有效,我不知道我是怎么得到这个的! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 2017-08-11
  • 2021-09-29
  • 2012-10-03
  • 1970-01-01
相关资源
最近更新 更多