2012-06-26 10 views
5

Sto monitorando in un file di configurazione dell'applicazione thread separato, che in alcuni casi può essere INI in un altro XML o in un altro. Codice di filo directory di monitoraggio (in Delphi) è qualcosa di simile:Perché ReadDirectoryChangesW, con solo il filtro FILE_NOTIFY_CHANGE_LAST_WRITE, segnala più di un evento sulla modifica dei file?

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; 

Perché segnala almeno 2 volte e su come ottenere il segnale corretto quando è stato cambiato un po 'la bandiera nel file di configurazione ed è stato salvato?

+0

A proposito: il filtro FILE_NOTIFY_CHANGE_LAST_ACCESS segnala solo una volta: QUAL È la differenza? – ALZ

risposta