Board logo

Subject: 建议 CnFileSystemWatcher 增加 Sender 这个参数 [Print This Page]

Author: 小帆    Time: 2009-8-6 22:44     Subject: 建议 CnFileSystemWatcher 增加 Sender 这个参数

在很多时候我们需要传递 Sender 使用,所以

TFileDealMethod = procedure(FileOperation: TFileOperation; const FileName1,    FileName2: string) of object;

最好改为:

TFileDealMethod = procedure(Sender: Tobject; FileOperation: TFileOperation; const FileName1,    FileName2: string) of object;

这样做的好处显而易见,可以将 Sender 参数传递给需要的时候使用,多个CnFileSystemWatcher 可以共用一个事件过称,靠 Sender 来控制各自的不同的设置。
Author: 小帆    Time: 2009-8-7 19:29

建议它改为这样:(红色部分为修改)
TFileDealMethod = procedure(FileOperation: TFileOperation; const FileName1,
    FileName2: string) of object;
改为:
TFileDealMethod = procedure(Sender: Tobject; FileOperation: TFileOperation; const FileName1,
    FileName2: string) of object;

TCnFileSystemWatcher = class;//增加

  TCnShellChangeThread = class(TThread)
  private
    FParent: TCnFileSystemWatcher;//增加
......
  public
    constructor Create(AParent:TCnFileSystemWatcher; ChangeEvent: TFileDealMethod); virtual;
.........
  end;

constructor TCnShellChangeThread.Create(AParent:TCnFileSystemWatcher; ChangeEvent: TFileDealMethod);
begin
............
FParent := AParent;//增加
  inherited Create(True);
end;

procedure TCnFileSystemWatcher.Start;
begin
  if csDesigning in ComponentState then
    Exit;
  if Assigned(FOnChange) then
  begin
    FThread := TCnShellChangeThread.Create(Self,FOnChange);
..............
end;

procedure TCnShellChangeThread.DoIOCompletionEvent;
var
  TempBuffer: TNotificationBuffer;
  FileOpNotification: PFileNotifyInformation;
  Offset: Longint;
  FileName1, FileName2: string;
  FileOperation: TFileOperation;
  procedure DoDirChangeEvent;
  var
    IsInFileMasks: Boolean;
  begin
    if Assigned(ChangeEvent) and FActive then
    begin
      if FTmpFileMasks.Count > 0 then
        IsInFileMasks := FileMatchesMasks(FileName1, FTmpFileMasks)
      else
        IsInFileMasks := FileMatchesMasks(FileName1, '*.*', False);
      if IsInFileMasks then
      begin
        if FIncludePath then
        begin
          FileName1 := GetTrueFileName(FDirectory + FileName1);
          if FileOperation = foRenamed then
            FileName2 := GetTrueFileName(FDirectory + FileName2);
        end;
        ChangeEvent(FParent, FileOperation, FileName1, FileName2);
      end;
    end;
  end;
  function GetFileName(const FileName: PWideChar; FileNameLength: DWORD):string;
end;

[ 本帖最后由 小帆 于 2009-8-7 19:30 编辑 ]
Author: Passion    Time: 2009-8-10 09:39

有道理,接受此增强修改。
感谢楼主的热心参与。
Author: solokey    Time: 2010-5-5 18:09

一直想改成Collection..一直懒得改....
还是靠自己多放几个组件吧....囧..




Welcome to CnPack Forum (http://bbs.cnpack.org/) Powered by Discuz! 5.0.0