加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 为什么只有FILE_NOTIFY_CHANGE_LAST_WRITE过滤器的Rea

发布时间:2020-12-15 09:47:16 所属栏目:大数据 来源:网络整理
导读:我在单独的线程应用程序配置文件中进行监视,在某些情况下,该文件可能是另一个 XML或另一个 XML中的INI. 线程监控目录代码(在Delphi中)是这样的: procedure TWatcherThread.Execute;type PFileNotifyInformation = ^TFileNotifyInformation; TFileNotifyInfo
我在单独的线程应用程序配置文件中进行监视,在某些情况下,该文件可能是另一个 XML或另一个 XML中的INI.
线程监控目录代码(在Delphi中)是这样的:

procedure TWatcherThread.Execute;
type
  PFileNotifyInformation = ^TFileNotifyInformation;
  TFileNotifyInformation = record
    NextEntryOffset: DWORD;
    Action: DWORD;
    FileNameLength: DWORD;
    FileName: WideChar;
  end;
const
  BufferLength = 65536;
var
  Filter,BytesRead: DWORD;
  InfoPointer: PFileNotifyInformation;
  Offset,NextOffset: DWORD;
  Buffer: array[0..BufferLength - 1] of byte;
  Overlap: TOverlapped;
  Events: array[0..1] of THandle;
  WaitResult: DWORD;
  FileName,s: string;
begin
  if fDirHandle <> INVALID_HANDLE_VALUE then begin
    Filter := FILE_NOTIFY_CHANGE_LAST_WRITE;

    FillChar(Overlap,SizeOf(TOverlapped),0);
    Overlap.hEvent := fChangeHandle;

    Events[0] := fChangeHandle;
    Events[1] := fShutdownHandle;

    while not Terminated do begin
      FillChar(Buffer,SizeOf(Buffer),0);
      if ReadDirectoryChangesW (fDirHandle,@Buffer[0],BufferLength,TRUE,Filter,@BytesRead,@Overlap,nil)
      then begin
        WaitResult := WaitForMultipleObjects(2,@Events[0],FALSE,INFINITE);
        if WaitResult = WAIT_OBJECT_0 then begin
          InfoPointer := @Buffer[0];
          Offset := 0;
          repeat
            NextOffset := InfoPointer.NextEntryOffset;
            FileName := WideCharToString(@InfoPointer.FileName);

            if (InfoPointer.Action = FILE_ACTION_MODIFIED) and (CompareText(FileName,'MyConfig.ini') = 0) then begin //read changes in config or INI file
              // Do Action.. refresh runtime flags
            end;
            PByte(InfoPointer) := PByte(DWORD(InfoPointer) + NextOffset);
            Offset := Offset + NextOffset;
          until NextOffset = 0;
        end;
      end;
    end;
  end;
end;

当配置文件中的某些标志被更改并保存时,为什么它至少发出2次信号以及如何获得正确的信号?

解决方法

Raymond Chen在他的博客上很好地解释了这一点: http://blogs.msdn.com/b/oldnewthing/archive/2014/05/07/10523172.aspx

简而言之“因为多件事被修改了”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读